#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<string.h>
int length(int *cop,char *cod,int x);
void lower(int *cop,char *cod,int num);
void upper(int *cop,char *cod,int num);
void digits(int *cop,char *cod,int num);
void symbol(int *cop,char *cod,int num);
int unique(int *cop,char *cod,int num);
void encryption(int x,char *epin,char *key);
void decode(char key,char *dpin,char *epin);
int main()
{
int cop[5];
char str[1000],cpin[200],epin[200];
int p;
int count ;
unsigned int pin,new_pin;
int valid_pin,attempt,i;
char choice;
pin=1;
do
{
i=1;
new_pin=1;
printf("Computer Engineering Safety Toolbox\n");
printf("User menu\n");
printf("s - set pin number\n");
printf("u - update pin number\n");
printf("e - exit\n");
printf("r - Password random generator\n");
printf("c - Check your password strength\n");
printf("input your choice :");
scanf("%s",&choice);
switch(choice)
{
case 's':
if(pin==1)
{
do
{
printf("\nSet pin number(5 digits) :"); //设置新密码
while(pin<10000||pin>99999)
{
scanf("%d",&pin);
break;
}
}while(pin<10000||pin>99999);
}
else
printf("\nyou have set your pin number!\n");
break;
case 'u':
if(pin==1)
{
printf("\nplease set your pin number first!\n");
}
else
printf("please input your old pin number :");
scanf("%d",&valid_pin);
if(valid_pin==pin)
{
while(new_pin<10000||new_pin>99999)
{
printf("\nplease input your new pin number(5 digits) :");
scanf("%d",&new_pin);
}
pin=new_pin;
break; //跳出while判断
}
else
while(i<=2)
{
printf("please input your old pin number again :") ;
scanf("%d",&valid_pin);
i++;
if(valid_pin==pin)
{
break;
}
else{}
}
if(valid_pin==pin)
{
printf("\nplease input your new pin number(5 digits) :");
scanf("%d",&pin);
printf("Password updated,press any key to return to the menu") ;
}
else
printf("\nYou have entered the wrong password three times, press any key to return to the menu");
getchar();
break;
case 'e':
break;
case 'r': //password random
printf("\nThe system can generate passwords randomly. The number of numbers, letters and symbols can be set below\n");
do{
do{
do{
p=0;
i=0; //printf("\nThe system can generate passwords randomly. The number of numbers, letters and symbols can be set below\n");
printf("When setting a password, ensure that the password contains 8 to 20 digits\n");
printf("\nPlease enter how many numbers you want in your password:") ;
scanf("%d",&count);
srand(time(0)); //current time as seed of random number generator
int i;
for (i = 0; i < count; i++)
{
int rand_num1 = rand() % 10;
str[i]='0' + rand_num1;
}
printf("\n")
;
p = p + count;
}while(p>20);
int case0;
printf("please select the method to create latter\n0 is small letter\n1 is small letter and capital\n2 is exit.\n");
printf("Please select the option you want :");
scanf("%d",&case0);
if(case0>=2)
{
count=0;
}
else
{
printf("\nPlease enter how many letters you want in your password:") ;
scanf("%d",&count);
}
int rand_num2;
for (i = p; i < count + p; i++)
{
if (case0 > 2)
break;
switch(case0)
{
case 0:
rand_num2 = rand() % 25 + 'a';
break;
case 1:
if (rand() % 2==0)
rand_num2 = rand() % 25 + 'a';
else
rand_num2 = rand() % 25 + 'A';
break;
}
str[i] = rand_num2;
}
printf("\n");
p = p + count;
}while(p>20);
printf("Please enter how many symbols you want in your password:") ;
scanf("%d",&count);
for (i = p; i < count+p; i++)
{
char rand_num3;
switch (rand() % 3)
{
case 0:
rand_num3 = rand() % 14 + '!';
break;
case 1:
rand_num3 = rand() % 7 + ':';
break;
case 2:
rand_num3 = rand() % 3 + '{';
break;
}
str[i]= rand_num3;
}
printf("\n");
p = p + count;
printf("The length of your password is %d\n",p);
getchar();
}while(p<8||p>20);
srand(time(NULL)); // 种个随机种子,以免随机数不变
for (i = p-1; i > 0; i--)
{
int index = rand() % i;
char temp;
temp = str[i];
str[i] = str[index];
str[index] = temp;
}
printf("\nyour password is ");
for (i=0;i<p;i++)
{
printf("%c",str[i]);
}
printf("\n\nclick Enter to return to the menu");
break;
case 'c':
{
int x,num;
int cop[5];
char cod[200];
x=0;
do{
num = length(cop,cod,x);
lower(cop,cod,num);
upper(cop,cod,num);
symbol(cop,cod,num);
digits(cop,cod,num);
x = unique(cop,cod,num);
}while(x<8||x>20);
switch(cop[0])
{
case 1:
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
case 2:
if(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1)
printf("\nthe password is middle\nclick Enter to return to the menu");
else
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
case 3:
if(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==1)
printf("\nthe password is strong\nclick Enter to return to the menu");
else
if((cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==0)||(cop[1]==1&&cop[2]==1&&cop[3]==1&&cop[4]==0&&cop[5]==1)||(cop[1]==1&&cop[2]==1&&cop[3]==0&&cop[4]==1&&cop[5]==1)||(cop[1]==1&&cop[2]==0&&cop[3]==1&&cop[4]==1&&cop[5]==1)||(cop[1]==0&&cop[2]==1&&cop[3]==1&&cop[4]==1&&cop[5]==1))
printf("\nthe password is middle\nclick Enter to return to the menu");
else
printf("\nthe password is weak\nclick Enter to return to the menu");
break;
}
break;
}
case 'k':
{
int x=0,i1=0;
char cpin[200],epin[x],key[x];
getchar();
printf("please input your password:");
while((cpin[x] = getchar())!='\n')
{
x=x+1;
}
printf("your length of password is %d\n",x);
encryption(x,epin,key);
printf("your key is %c\n",*key);
printf("your encrypted password is ");
for(i1=0;i1<x;i1++)
{
printf("%c",epin[i1]);
}
printf("\nclick Enter to return to the menu");
break;
}
case 'd':
{
int x,i;
char key,dpin[x],epin[x];
printf("please input your key:");
scanf("%c",&key);
getchar();
decode(key,dpin,epin);
printf("your encrypted password is ");
for(i=0;i<x;i++)
{
printf("%c",dpin[i]);
}
printf("\nclick Enter to return to the menu\n"); getchar();
break;
}
default:
{
printf("Not available\n");
break;
}
}
printf("\n");
getchar();
}while(choice!='e' && choice!='E');
printf("\nThank you for using the toolbox");
return 0;
}
int length(int *cop,char *cod,int x)
{
int num=0;
do{
x=0 ;
printf("please input your password(8~20):");
getchar();
while((cod[x] = getchar())!='\n')
{
x=x+1;
}
num = x;
if(num>8&&num<=10)
cop[0]=1;
else if(num>10&&num<16)
cop[0]=2;
else
cop[0]=3;
}while(num<=8);
return num;
}
void lower(int *cop,char *cod,int num)
{
int i;
for(i=0;i<=num;i++)
{
if(cod[i]>='a'&&cod[i]<='z')
{
cop[1]=1;
break;
}
else
cop[1]=0;
}
num = num;
}
void upper(int *cop,char *cod,int num)
{
int i;
for(i=0;i<=num;i++)
{
if(cod[i]>='A'&&cod[i]<='Z')
{
cop[2]=1;
break;
}
else
cop[2]=0;
}
}
void digits(int *cop,char *cod,int num)
{
int i;
for(i=0;i<=num;i++)
{
if(cod[i]>='0'&&cod[i]<='9')
{
cop[3]=1;
break;
}
else
cop[3]=0;
}
}
void symbol(int *cop,char *cod,int num)
{
int i;
for(i=0;i<=num;i++)
{
if((cod[i]>='!'&&cod[i]<='/')||(cod[i]>=':'&&cod[i]<='@')||(cod[i]>='['&&cod[i]<='-')||(cod[i]>='{'&&cod[i]<='~'))
{
cop[4]=1;
break;
}
else
cop[4]=0;
}
}
int unique(int *cop,char *cod,int num)
{
int i;
for(i=0;i<num;i++)
{
int j;
for(j=i+1;j<=num;j++)
{
if(cod[i]==cod[j])
{
cop[5]=0;
break;
}
else
cop[5]=1;
}
if(cop[5]==0)
break;
else{}
}
return num;
}
void encryption(int x,char *epin,char *key)
{
char cpin[200];
int i=0;
srand(time(0));
for(i=0;i<x;i++)
{
do{
*key=rand()%94+'!';
epin[i]=cpin[i]^*key;
}while(epin[i]<'!');
}
}
void decode(char key,char *dpin,char *epin)
{
int i,;
for(i=0;i<x;i++)
{
dpin[i]=epin[i]^key;
}
}
你的encryption函数中,x是输入参数,这个参数你如果还想返回的话,可以把void encryption函数改成int encryption,在最后return x就可以了。
encryption函数的参数epin是一个字符指针,你在main函数中申请了字符数组epin[200],在main含中调用encryption的时候,直接把epin传递给encryption函数就可以了,这样就可以直接把加密后的数据获取出来,不需要再做别的处理了。
可以在for循环结束后,加一句:epin[i] = 0;保证返回的加密字符串能正确结束