當前位置:首頁 » 編程語言 » c語言編制密碼生成器
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言編制密碼生成器

發布時間: 2022-07-31 16:40:00

⑴ 如何使用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();
}

⑵ 如何用C語言編寫密碼程序

1、用一個字元數組來存密碼
再用一個字元數組接收你的輸入,然後用strcmp
來比較,如果返回0則密碼是正確的
2、常式:

#include"stdio.h"
#include"string.h"
intmain()
{
charmima[100]="YuanShi888";
charinput[100]={0};
printf("請輸入密碼:");
gets(input);
if(strcmp(mima,input)==0)
printf("恭喜你,密碼正確! ");
else
printf("對不起,密碼輸入錯誤! ");

}

⑶ 用c語言生成八位的隨機密碼

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
char pool[]=
{
Ɔ',Ƈ',ƈ',Ɖ',Ɗ',Ƌ',ƌ',ƍ',Ǝ',Ə',
'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z','A','B','C','D',
'E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X',
'Y','Z'
};//隨機池
srand(time(0));
char pwd[9];
pwd[8]=''//方便作為字元串輸出處理
int i=0;
while(i!=8)
{
pwd[i++]=pool[rand()%sizeof(pool)];
}
printf("密碼%s",pwd);

}

⑷ 如何用c語言來編一個簡單的密碼程序

這個問題很難解釋啊~如果最簡單的說,你有一個固定的密碼,比如123
那麼直接就是
if(password==123){
//你要輸出的正確信息,比如cout
?????????
}
else{
//錯誤信息
}
復雜來說,你的密碼可能存放在一個資料庫中,你就要先在資料庫中查找對應的用戶名,再核對密碼
此外,密碼一般是不能直接保存的,會被看到,所以你要有個加密的演算法來保存你的密碼,簡單來說,你把正確的密碼加密後保存,你等他輸入一個密碼,你用同樣的演算法加密,再和你的保存的加密過的比較就OK了

⑸ 怎麼用c語言寫一個創建用戶名和密碼並且能修改密碼的程序

#include <stdio.h>//我自己做的,你拿去用吧!
#include <string.h>
struct e
{
char a[10];
char b[10];
}z;
int main()
{ int t=0;
char s[10],d[10];
FILE *p;
void as();
if ((p=fopen("m.txt","r+"))==NULL)
{
p=fopen("m.txt","w+");
t=1;
}
if(t==1)
{
printf("當前沒有任何用戶\n");
printf("請新建用戶名: ");
scanf("%s",s);
printf("為用戶設置密碼: ");
scanf("%s",d);
strcpy(z.a,s);
strcpy(z.b,d);
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
}
if(t==0)
{
printf("請輸入用戶名: ");
scanf("%s",s);
fscanf(p,"%s %s",z.a,z.b);
fclose(p);
if (!strcmp(z.a,s))
{
printf("請輸入密碼:");
scanf("%s",d);getchar();
if(!strcmp(z.b,d))
{ char i;
printf("是否要修改密碼?(輸入y修改,n退出!)");
scanf("%c",&i);
if(i=='y')
{
printf("請輸入修改密碼:");
scanf("%s",z.b);
p=fopen("m.txt","w+");
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
printf("修改成功!");

}

}
else printf("密碼錯誤!");
}
else printf("用戶名錯誤");
fclose(p);

}

}

⑹ 用c語言如何編寫一個能生成動態密碼的那種程序

#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <time.h>
void main()
{
srand( (unsigned)time( NULL ) );
for(int i=0;i<100000;i++)
{
cout<<rand()<<endl;
}
}

⑺ 如何用C語言編一個密碼生成器

C語言實現密碼生成器,參考代碼如下:

#include

#include
#include


//constcharlower_chars[]="abcdefghijklmnopqrstuvwxyz";
//constcharupper_chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//constcharnumber_chars[]="0123456789";
constcharlower_chars[]="abcdefghijkmnpqrstuvwxyz";//noloro
constcharupper_chars[]="ABCDEFGHJKLMNPQRSTUVWXYZ";//noIorO
constcharnumber_chars[]="23456789";//no1or0
constcharspecial_chars[]="!@#$%^&*()-=_+[]{};:'"<>,.?/";


constint_ks_pass_len=17;


voidmkpass(charpass[_ks_pass_len+1])
{
inti=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;i<j;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]='';
}


int_tmain(intargc,_TCHAR*argv[])
{
srand(time(0));
charszPass[_ks_pass_len+1];
for(inti=0;i<16;i++)
{
mkpass(szPass);
printf("%s ",szPass);
}
return0;
}<j;i&#43;&#43;)

⑻ 用c語言編寫一個設置密碼的程序

#include "stdio.h"
int* set(void);
int* set(void)
{
int i;
static a[4];
printf("請輸入要設置的4位密碼 :");
for(i=0;i<4;i++)
{
scanf("%d",&a[i]);
}
return a;
}

int main()
{
int i,*a,b[4];
a=set();
printf("請輸入4位密碼 :");
for(i=0;i<4;i++)
{
scanf("%d",&b[i]);
}
i=0;
while(i<4)
{
if(a[i]==b[i])
{
printf("%d",a[i]);
i++;
}
else
{
break;
}
}
if(i==4)
{
printf("密碼正確\n");
}
else
{
printf("密碼錯誤\n");
}
return 0;

}