Go to Top

Wednesday 7 September 2011

Utilities 4 - Captcha

This is just a small code resembling the Captcha codes seen on different websites.

Java Program : 


import java.io.*;
import java.util.Random;
class captcha
{
 static String s,capt="",data="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 static void main()throws IOException
 {
     capt="";
     System.out.print("\f");
     generate();
     input();
     match();
    }
    static void generate()
    {
        Random r=new Random();
        char k;
        int i,c,l=data.length();
        System.out.print("Captcha : ");
        for(i=1;i<=5;i++)
        {
            c=r.nextInt(l);
            k=data.charAt(c);
            capt=capt+k;
            System.out.print(k+" ");
        }
    }
    static void input()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\n\nPlease enter the code as you see above( w/o spaces ) : ");
        s=br.readLine();
    }
    static void match()throws IOException
    {
        if(s.equals(capt))
        System.out.print("\nCaptch match");
        else
        {
            System.out.print("\nCaptcha mismatch");
            try
            {
            Thread.sleep(3000);}
            catch(InterruptedException e)
            {}
            main();
        }
    }
}

No comments:

Post a Comment

ShareThis