📄 vigenere.cpp
字号:
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<math.h>
void menu()/*菜单*/
{
printf("===============================================================================");
printf(" Encrypt the file enter 1 \n");
printf(" Decrypt the file enter 2 \n");
printf(" Quit enter 3 \n");
printf("===============================================================================");
printf(" Please select a item: ");
return;
}
char encrypt(char ch1,char ch2)/*加密程序*/
{
if(ch1>='A'&&ch1<='Z'&&ch2>='A'&&ch2<='Z')
{
return 'A'+(ch1-'A'+ch2-'A')%26;
}
if(ch1>='a'&&ch1<='z'&&ch2>='a'&&ch2<='z')
{
return 'a'+(ch1-'a'+ch2-'a')%26;
}
return ch1;
}
char decrypt(char ch1,char ch2)/*解密程序*/
{
if(ch1>='A'&&ch1<='Z'&&ch2>='A'&&ch2<='Z')
{
return 'A'+(ch1-'A'+26-(ch2-'A'))%26;
}
if(ch1>='a'&&ch1<='z'&&ch2>='a'&&ch2<='z')
{
return 'a'+(ch1-'a'+26-(ch2-'a'))%26;
}
return ch1;
}
void main()
{
int i;
char key[5];
char ch;
FILE *in,*out;
char infile[10],outfile[10];
void textbackground(int BLACK);
void textcolor(int LIGHTGREEN);
Sleep(3);
menu();
ch=getch();
while(ch!='3')
{
if(ch=='1')
{
printf(" Please input the infile: \n");
scanf("%s",infile);/*输入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile! \n");
printf("Press any key to exit! \n");
getch();
exit(0);
}
printf(" Please input the key: \n");
scanf("%s",key);/*输入加密密码*/
printf(" Please input the outfile: \n");
scanf("%s",outfile);/*输入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile! \n");
printf("Press any key to exit! \n");
fclose(in);
getch();
exit(0);
}
i=0;
while(!feof(in)) //当不是文件的结尾时
{
fputc(encrypt(fgetc(in),key[i%strlen(key)]),out); //存在问题!!! 为何生成密文的结尾都有怪异的符号?
i++;
}
fclose(in);
fclose(out);
printf("Encrypt successful! \n");
Sleep(1);
}
if(ch=='2')
{
printf(" Please input the infile: \n");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile! \n");
printf("Press any key to exit! \n");
getch();
exit(0);
}
printf(" Please input the key: \n");
scanf("%s",key);/*输入解密密码*/
printf(" Please input the outfile: \n");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile! \n");
printf("Press any key to exit! \n");
fclose(in);
getch();
exit(0);
}
i=0;
while(!feof(in))
{
fputc(decrypt(fgetc(in),key[i%strlen(key)]),out);
i++;
}
fclose(in);
fclose(out);
printf("Decrypt successful! \n");
Sleep(1);
}
printf("\n");
menu();
ch=getch();
}
printf("Good Bye! \n");
Sleep(3);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -