本文目录一览:
如何用C语言编写密码程序
1、用一个字符数组来存密码
再用一个字符数组接收你的输入,然后用strcmp
来比较,如果返回0则密码是正确的
2、例程:
#include "stdio.h"
#include "string.h"
int main()
{
char mima[100]="YuanShi888";
char input[100]={0};
printf("请输入密码:");
gets(input);
if(strcmp(mima,input)==0)
printf("恭喜你,密码正确!\n");
else
printf("对不起,密码输入错误!\n");
}
如何用C语言编一个密码生成器
C语言实现密码生成器,参考代码如下:
#include
#include
#include
//const char lower_chars[] = "abcdefghijklmnopqrstuvwxyz";
//const char upper_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//const char number_chars[] = "0123456789";
const char lower_chars[] = "abcdefghijkmnpqrstuvwxyz"; // no l or o
const char upper_chars[] = "ABCDEFGHJKLMNPQRSTUVWXYZ"; // no I or O
const char number_chars[] = "23456789"; // no 1 or 0
const char special_chars[] = "!@#$%^*()-=_+[]{};:'\",.?/";
const int _ks_pass_len = 17;
void mkpass(char pass[_ks_pass_len+1])
{
int i = 0, j = 0, k = 0,n = 0;
n = _ks_pass_len/4;
for (; i n; i++)
{
pass[i] = lower_chars[rand()%(strlen(lower_chars))];
pass[i+n] = upper_chars[rand()%(strlen(upper_chars))];
pass[i+2*n] = number_chars[rand()%(strlen(number_chars))];
pass[i+3*n] = special_chars[rand()%(strlen(special_chars))];
}
j = _ks_pass_len - 4*n;
for (i = 0; ij; i#43;#43;) {
pass[i+4*n] = special_chars[rand()%(strlen(special_chars))];
}
//字符乱序
for (i = 0; i 32; i++)
{
j = rand()%(_ks_pass_len);
k = pass[j];
pass[j] = pass[i%_ks_pass_len];
pass[i%_ks_pass_len] = k;
}
pass[_ks_pass_len] = '\0';
}
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(0));
char szPass[_ks_pass_len+1];
for (int i=0; i16; i++)
{
mkpass(szPass);
printf("%s\n",szPass);
}
return 0;
}j; i#43;#43;)
如何使用c语言编写一个密码程序
密码保存在文件中,从文件中读取密码,但是没做容错和异常处理,仅供参考
#include stdio.h
#include string.h
#define PSDLEN 6
void inputPsd(char *str) /*处理输入*/
{
int i;
for(i = 0; i PSDLEN; i++)
{
while(1)
{
str[i] = getch();
if(str[i] == '\b') /*处理退格键*/
{
i--;
if(i 0)
{
i = 0;
}
else
{
printf("\b \b");
}
continue;
}
else if(str[i] == '\r') /*处理回车键*/
{
continue;
}
else
{
printf("*");
break;
}
}
}
str[i] = '\0';
printf("\n");
}
int checkFirst() /*检测是否是第一次使用*/
{
FILE *fp;
if((fp = fopen("psd.dat", "rb")) == NULL)
{
return 1;
}
fclose(fp);
return 0;
}
void firstUse() /*第一次使用 需要输入密码*/
{
FILE *fp;
int i;
char passwd[PSDLEN + 1];
char checkPsd[PSDLEN + 1];
if((fp = fopen("psd.dat", "wb")) == NULL)
{
printf("Creat password error!\n");
exit(1);
}
while(1)
{
printf("Please input password:");
inputPsd(passwd);
printf("\nPlease input password again:");
inputPsd(checkPsd);
if(!strcmp(passwd, checkPsd))
{
break;
}
printf("\ncheck password error! \n");
}
fwrite(passwd, sizeof(char), PSDLEN, fp);
fclose(fp);
}
void login() /*核对密码,并登录*/
{
FILE *fp;
int i, num = 3;
char passwd[PSDLEN + 1];
char checkPsd[PSDLEN + 1];
if((fp = fopen("psd.dat", "rb")) == NULL)
{
puts("Open psd.dat error");
exit(1);
}
fread(passwd, sizeof(char), PSDLEN, fp);
fclose(fp);
passwd[PSDLEN] = '\0';
printf("Please input password to login");
while(num)
{
printf("you have %d chances to cry:\n", num);
inputPsd(checkPsd);
if(!strcmp(passwd, checkPsd))
{
break;
}
puts("\npassword error,Please input again");
num--;
}
if(!num)
{
puts("Press any key to exit...");
getch();
exit(0);
}
else
{
puts("\n--------\nWelcome!\n--------\n");
}
}
void main()
{
if(checkFirst())
{
firstUse();
}
else
login();
getch();
}