标题:【Code§C/C++】密码程序   出处:Felix021 时间:Tue, 28 Aug 2007 22:16:16 +0000 作者:felix021 地址:https://www.felix021.com/blog/read.php?275 内容: 【Code§C/C++】密码程序 这个程序可以修改密码,依靠一个文件来保存的 密码有经过简单的加密 #include #include #include int menu(); int verify(int choice); int change(); int reboot(); int write(char *pass); int read(char *pass); FILE *fp=NULL; int main() { int choice=0,a,b,c; while(choice<1||choice>3)choice=menu(); switch(choice) { case 1: a=verify(1); if(a) { printf("Right password^_^\n"); return 0; } else reboot(); break; case 2: a=verify(2); if(a)change(); else reboot(); break; case 3: reboot(); break; default: reboot(); } return 0; } int menu() { char input[200]; int i; printf("\n\nWhat do you want to do?\n"); printf("\t1. Enter password and use MSDOS\n"); printf("\t2. Change password\n"); printf("\t3. I don't know the password, reboot\n"); printf("Input your choice: "); fflush(stdin); gets(input); fflush(stdin); for(i=0;input[i];i++) if(input[i]<='9'&input[i]>='1')break; switch(input[i]) { case '1':return 1; case '2':return 2; case '3':return 3; default :return 0; } } int verify(int choice) { if((fp=fopen("pass.dat","rb+"))==NULL) { /*无密码文件,创建*/ printf("Currently there's no password.\n"); if((fp=fopen("pass.dat","w"))==NULL) { /*无法创建*/ printf("Unable to create a file."); return 0; } else { /*建立好了文件,修改密码*/ fclose(fp); if(choice==2)return 1; return change(); } } else /*密码文件存在*/ { int times=0; char pass[20]; char temp[20]; read(temp); while(times<3) { printf("Please input the password: "); gets(pass); fflush(stdin); if(strcmp(pass,temp)==0)return 1; else printf("Bad password.\n"); times++; } printf("\nYou have wrongly inputted the password 3 times."); return 0; } } int change() { char pass1[20]; char pass2[20]; if((fp=fopen("pass.dat","w"))==NULL) { /*无法创建*/ printf("Unable to create a file."); return 0; } else { changepass: printf("Please enter new password: "); gets(pass1); if(pass1[0]==0) goto changepass; fflush(stdin); printf("Please confirm the password: "); gets(pass2); fflush(stdin); if(strcmp(pass1,pass2)==0) { write(pass1); return 1; } else { printf("Two passwords don't match!\n"); goto changepass; } } } int reboot() { printf("\nThis computer will reboot now.\n"); system("pause"); system("off r"); } int read(char *pass) { int i=0; fread(pass,sizeof(char),20,fp); fclose(fp); for(i=0;i<20;i++) pass[i]-=i; return 0; } int write(char *pass) { int i=0; for(i=0;i<20;i++) pass[i]+=i; fwrite(pass,sizeof(char),20,fp); fclose(fp); return 0; } Generated by Bo-blog 2.1.0