编写函数加密字符c语言,新手求指导,C语言加密字符串解决办法

当前位置:我的异常网» C++ » 新手求指导,C语言加密字符串解决办法

新手求指导,C语言加密字符串解决办法

www.myexceptions.net  网友分享于:2013-02-23  浏览:32次

新手求指导,C语言加密字符串

最近写了一个密码验证的程序,只差最后加密部分了

我现在有一个密码字符串pw1是存储在txt里面的

密码是用户自行输入和创建的

现在我想把密码加密之后再存储到txt,求各位大神帮助啊

主要问题是我需要知道加密函数encrypt和解密函数decrypt怎么写

以下是源码:

#include

#include

#include

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)

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值