新手求指导,C语言加密字符串
最近写了一个密码验证的程序,只差最后加密部分了
我现在有一个密码字符串pw1是存储在txt里面的
密码是用户自行输入和创建的
现在我想把密码加密之后再存储到txt,求各位大神帮助啊
主要问题是我需要知道加密函数encrypt和解密函数decrypt怎么写
以下是源码:
#include <stdio.h>
#include <conio.h>
#include <string.h>
int log_in(FILE *password,char pw1[100],char pw2[100]);
void new_pwd(FILE *password,char pw1[100],char pw2[100]);
void enter(char *ent);
void encrypt(char *pwd);
void menu(void);
int main(void)
{
FILE *password;
char pw1[100],pw2[100];
char n;
password=fopen("pass.txt","r");
if(log_in(password,pw1,pw2)!=1)
return 0;
for(;;)
{
system("cls");
menu();
n=getch();
system("cls");
switch(n)
{
case '1':
password=fopen("pass.txt","w+");
new_pwd(password,pw1,pw2);
break;
case '2':
printf("Maneger System logout!\n");
return 0;
default:
printf("Error: Please choose an operation between 1-2\n\n");
printf("Press any key to go to the menu");
getch();
break;
}
}
}
int log_in(FILE *password,char pw1[100],char pw2[100])
{
int loop;
system("cls");
printf("Login to Maneger System\n\n");
if(password==NULL)
{
password=fopen("pass.txt","w+");
printf("This is your first time to login, please set password!\n\n");
new_pwd(password,pw1,pw2);
return 1;
}
else
{
char *n=pw1,i;
while((i=fgetc(password))!=EOF)
{
*n=i;
n++;
}
*n='\0';
fclose(password);
for(loop=1;loop<=5;loop++)
{
printf("Please enter your password\n> ");
enter(pw2);
encrypt(pw2);
fflush(stdin);
if(strcmp(pw1,pw2)!=0)
{
if(loop<5)
{
system("cls");
printf("Your password is wrong! You have %d chances to try!\n\n",5-loop);
}
else
{
system("cls");
printf("You have no chance left! Press any key to exit!");
getch();
system("cls");
printf("Maneger System logout!\n");
return 0;
}
}
else
break;
}
}
return 1;
}
void new_pwd(FILE *password,char pw1[100],char pw2[100])
{
for(;;)
{
printf("Please enter your password\n> ");
enter(pw1);
encrypt(pw1);
fflush(stdin);
printf("Please enter your password again\n> ");
enter(pw2);
encrypt(pw2);
fflush(stdin);
if(strcmp(pw1,pw2)==0)
break;
else
{
printf("\nError: The passwords you entered were not the same!\n");
printf("Please press any key to try again!");
}
getch();
system("cls");
}
printf("Your password is set as * %s *\n",pw1);
printf("Press any key to go to the menu");
fputs(pw1,password);
fclose(password);
getch();
}
void enter(char *ent)
{
int i=0,j=0;
char c;
for(;;)
{
c=getch();
if(j==0)
{
if((int)c==13)
{
system("cls");
printf("You must enter a password!\n\n");
printf("Please enter your password\n> ");
}
else if((int)c==8)
*ent='\0';
else
{
j=1;
*ent=c;
ent++;
i++;
printf("*");
}
}
else
{
if(i>=29&&(int)c!=13&&(int)c!=8)
{
*ent='\0';
}
else if((int)c==8)
{
if(i>0)
{
i--;
ent--;
*ent='\0';
printf("\b \b");
}
else
{
j=0;
*ent='\0';
}
}
else if((int)c==13)
break;
else
{
*ent=c;
ent++;
i++;
printf("*");
}
}
}
*ent='\0';
printf("\n");
}
void encrypt(char *pwd)
{
char str[100];
int i;
str[100]=*pwd;
for(i=0;i!='\0';i++)
str[i]+=5;
*pwd=str[100];
}
void menu(void)
{
printf("*************** Maneger System ***************\n");
printf("* 1. Change the password *\n");
printf("* 2. Exit *\n");
printf("**********************************************\n\n");
printf("Please choose an operation > ");
}