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

wang110117 2011-12-12 04:37:02
最近写了一个密码验证的程序,只差最后加密部分了
我现在有一个密码字符串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 > ");
}
...全文
336 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang110117 2011-12-13
  • 打赏
  • 举报
回复
感谢各位的关注~~~问题已经解决了~~~结贴~~~
wang110117 2011-12-12
  • 打赏
  • 举报
回复
我现在遇到的主要问题是
如果我加密了,那么在创建新密码时,程序说我两次输入的密码不一致
yuucyf 2011-12-12
  • 打赏
  • 举报
回复
一般使用MD5进行加密.
quwei197874 2011-12-12
  • 打赏
  • 举报
回复
算法是由楼主自己决定的
ouyh12345 2011-12-12
  • 打赏
  • 举报
回复
有很多种加密算法,先选一个吧
网络上应该有很多例子

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧