Program to generate CAPTCHA and verify user
A CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a test to determine whether the user is human or not.
So, the task is to generate unique CAPTCHA every time and to tell whether the user is human or not by asking user to enter the same CAPTCHA as generated automatically and checking the user input with the generated CAPTCHA.
Examples:
CAPTCHA: x9Pm72se
Input: x9Pm62es
Output: CAPTCHA Not Matched
CAPTCHA: cF3yl9T4
Input: cF3yl9T4
Output: CAPTCHA Matched
The set of characters to generate CAPTCHA are stored in a character array chrs[] which contains (a-z, A-Z, 0-9), therefore size of chrs[] is 62.
To generate a unique CAPTCHA every time, a random number is generated using rand() function (rand()%62) which generates a random number between 0 to 61 and the generated random number is taken as index to the character array chrs[] thus generates a new character of captcha[] and this loop runs n (length of CAPTCHA) times to generate CAPTCHA of given length.
Algorithm:
- First declare and define the checkCaptcha() function that takes two string parameters and returns a boolean value.
- Within the checkCaptcha() function, compare the two string parameters using the compare() function and return true if they are the same; otherwise, return false.
- Declare and define the generateCaptcha() function that takes an integer parameter and returns a string value.
- Within the generateCaptcha() function, initialize a time variable using the time() function and seed the random number generator using the srand() function.
- Declare a character array containing all the characters to be included in the CAPTCHA and assign it to a char pointer variable.
- Generate a random CAPTCHA string of the specified length by repeatedly appending random characters from the character array to a string variable using the push_back() function.
- Return the generated CAPTCHA string.
- Within the main() function, declare a string variable named captcha and call the generateCaptcha() function with a length of 9 to generate a random CAPTCHA string.
- Print the generated CAPTCHA string on the console.
CPP
#include <bits/stdc++.h>
using namespace std;
bool checkCaptcha(string& captcha, string& user_captcha)
{
return captcha.compare(user_captcha) == 0;
}
string generateCaptcha( int n)
{
time_t t;
srand ((unsigned) time (&t));
char * chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHI"
"JKLMNOPQRSTUVWXYZ0123456789" ;
string captcha = "" ;
while (n--)
captcha.push_back(chrs[ rand () % 62]);
return captcha;
}
int main()
{
string captcha = generateCaptcha(9);
cout << captcha;
string usr_captcha;
cout << "\nEnter above CAPTCHA: " ;
cin >> usr_captcha;
if (checkCaptcha(captcha, usr_captcha))
printf ( "\nCAPTCHA Matched" );
else
printf ( "\nCAPTCHA Not Matched" );
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG
{
static boolean checkCaptcha(String captcha, String user_captcha)
{
return captcha.equals(user_captcha);
}
static String generateCaptcha( int n)
{
Random rand = new Random( 62 );
String chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
String captcha = "" ;
while (n--> 0 ){
int index = ( int )(Math.random()* 62 );
captcha+=chrs.charAt(index);
}
return captcha;
}
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
String captcha = generateCaptcha( 9 );
System.out.println(captcha);
System.out.println( "Enter above CAPTCHA: " );
String usr_captcha = reader.readLine();
if (checkCaptcha(captcha, usr_captcha))
System.out.println( "CAPTCHA Matched" );
else
System.out.println( "CAPTCHA Not Matched" );
}
}
|
Python3
import random
def checkCaptcha(captcha, user_captcha):
if captcha = = user_captcha:
return True
return False
def generateCaptcha(n):
chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
captcha = ""
while (n):
captcha + = chrs[random.randint( 1 , 1000 ) % 62 ]
n - = 1
return captcha
captcha = generateCaptcha( 9 )
print (captcha)
print ( "Enter above CAPTCHA:" )
usr_captcha = input ()
if (checkCaptcha(captcha, usr_captcha)):
print ( "CAPTCHA Matched" )
else :
print ( "CAPTCHA Not Matched" )
|
C#
using System;
using System.Text;
class GFG
{
static bool CheckCaptcha( string captcha, string user_captcha)
{
return captcha.Equals(user_captcha);
}
static string GenerateCaptcha( int n)
{
Random rand = new Random();
string chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
StringBuilder captcha = new StringBuilder();
while (n-- > 0)
{
int index = rand.Next(62);
captcha.Append(chrs[index]);
}
return captcha.ToString();
}
static void Main( string [] args)
{
string captcha = GenerateCaptcha(9);
Console.WriteLine(captcha);
Console.WriteLine( "Enter above CAPTCHA: " );
string usr_captcha = Console.ReadLine();
if (CheckCaptcha(captcha, usr_captcha))
Console.WriteLine( "CAPTCHA Matched" );
else
Console.WriteLine( "CAPTCHA Not Matched" );
}
}
|
Javascript
function checkCaptcha(captcha, user_captcha) {
return captcha.localeCompare(user_captcha) == 0;
}
function generateCaptcha(n) {
const chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
let captcha = "" ;
for (let i = 0; i < n; i++) {
captcha += chrs[(Math.floor(Math.random() * chrs.length))];
}
return captcha;
}
function main() {
const captcha = generateCaptcha(9);
console.log(captcha);
const usr_captcha = prompt( "Enter above CAPTCHA:" );
if (checkCaptcha(captcha, usr_captcha))
console.log( "CAPTCHA Matched" );
else
console.log( "CAPTCHA Not Matched" );
}
main();
|
Output:
CAPTCHA: cF3yl9T4
Enter CAPTCHA: cF3yl9T4
CAPTCHA Matched
Time Complexity: O(n)
Space Complexity: O(1)
Last Updated :
03 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...