生成验证码并验证用户身份的程序

A. 验证码 (区分计算机和人类的完全自动化公共图灵测试)是一种确定用户是否为人类的测试。 因此,任务是每次生成唯一的验证码,并通过要求用户输入自动生成的同一验证码并用生成的验证码检查用户输入来判断用户是否为人类。 例如:

null
CAPTCHA: x9Pm72seInput: x9Pm62esOutput: CAPTCHA Not MatchedCAPTCHA: cF3yl9T4Input: cF3yl9T4Output: CAPTCHA Matched

生成CAPTCHA的字符集存储在字符数组chrs[]中,该数组包含(a-z,a-z,0-9),因此chrs[]的大小为62。 为了每次生成唯一的验证码,使用rand()函数(rand()%62)生成一个随机数,该函数生成一个介于0到61之间的随机数,生成的随机数作为字符数组chrs[]的索引,从而生成一个新的验证码[]字符,该循环运行n次(验证码长度)以生成给定长度的验证码。

CPP

// C++ program to automatically generate CAPTCHA and
// verify user
#include<bits/stdc++.h>
using namespace std;
// Returns true if given two strings are same
bool checkCaptcha(string &captcha, string &user_captcha)
{
return captcha.compare(user_captcha) == 0;
}
// Generates a CAPTCHA of given length
string generateCaptcha( int n)
{
time_t t;
srand ((unsigned) time (&t));
// Characters to be included
char *chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHI"
"JKLMNOPQRSTUVWXYZ0123456789" ;
// Generate n characters from above set and
// add these characters to captcha.
string captcha = "" ;
while (n--)
captcha.push_back(chrs[ rand ()%62]);
return captcha;
}
// Driver code
int main()
{
// Generate a random CAPTCHA
string captcha = generateCaptcha(9);
cout << captcha;
// Ask user to enter a CAPTCHA
string usr_captcha;
cout << "Enter above CAPTCHA: " ;
cin >> usr_captcha;
// Notify user about matching status
if (checkCaptcha(captcha, usr_captcha))
printf ( "CAPTCHA Matched" );
else
printf ( "CAPTCHA Not Matched" );
return 0;
}


Python3

# Python program to automatically generate CAPTCHA and
# verify user
import random
# Returns true if given two strings are same
def checkCaptcha(captcha, user_captcha):
if captcha = = user_captcha:
return True
return False
# Generates a CAPTCHA of given length
def generateCaptcha(n):
# Characters to be included
chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
# Generate n characters from above set and
# add these characters to captcha.
captcha = ""
while (n):
captcha + = chrs[random.randint( 1 , 1000 ) % 62 ]
n - = 1
return captcha
# Driver code
# Generate a random CAPTCHA
captcha = generateCaptcha( 9 )
print (captcha)
# Ask user to enter a CAPTCHA
print ( "Enter above CAPTCHA:" )
usr_captcha = input ()
# Notify user about matching status
if (checkCaptcha(captcha, usr_captcha)):
print ( "CAPTCHA Matched" )
else :
print ( "CAPTCHA Not Matched" )
# This code is contributed by shubhamsingh10


输出:

CAPTCHA: cF3yl9T4Enter CAPTCHA: cF3yl9T4CAPTCHA Matched

本文由 希曼苏·古普塔(巴格里) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享