本文目錄一覽:
如何用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();
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/199407.html