#include
#include
void Encrypt(char *filename);
void Decrypt(char *filename);
int main()
{
char filename[20];
int select;
printf("1:Encryption \n2:Decryption \n Please select:");
scanf("%d",&select);
switch(select)
{
case 1: printf("Please input the filename you want to encrypt:");
scanf("%s",filename);
Encrypt(filename);
break;
case 2: printf("Please input the filename you want to decrypt:");
scanf("%s",filename);
Decrypt(filename);
break;
default:printf("Input wrong!"); }
return 0;
}
void Encrypt(char *filename)
{
char ch;
unsigned int s; //密钥种子
unsigned char k; //密钥流
printf("Please input a sand seed:");
scanf("%u",&s);
srand(s); //初始化随机数
FILE*fp1,*fp2;
if((fp1=fopen(filename,"r"))==NULL)
{
printf("The ciphertext create failure!\n");
exit(0);
}
if((fp2=fopen("miwen.txt","w"))==NULL)
{
printf("The ciphertext open failure!\n");
exit(0);
}
ch=fgetc(fp1);
while(ch!=EOF)
{
k=rand()%6;
ch=ch^k;
fputc(ch,fp2); //写入加密文件
ch=fgetc(fp1); //读取下一个字符
}//加密过程
printf("The plaintext is encrypted successfully!please check in miwen.txt\n");
fclose(fp1);
fclose(fp2);
}
void Decrypt(char *filename) { char ch; unsigned char k; //密钥流 unsigned int s; //密钥种子 FILE *fp1,*fp2; printf("Please input a srand seed:"); scanf("%u",&s); srand(s); //初始化随机数 if((fp1=fopen(filename,"r"))==NULL) { printf("The ciphertext open failure!\n"); exit(0); } if((fp2=fopen("jiemi.txt","w"))==NULL) { printf("The restore plaintext open failure!\n"); exit(0); } ch=fgetc(fp1); k=rand()%6; while(ch!=EOF) { ch=ch^k; fputc(ch,fp2); //写入解密文件 putchar(ch); //输出到屏幕 ch=fgetc(fp1); //读取下一个字符 k=rand()%6; }//解密过程 printf("The plaintext is decrypted successfully!please check in jiemi.txt\n"); fclose(fp1); fclose(fp2); }