📄 jiami.cpp
字号:
//本程序是实现Vigenere加密算法的小程序
#include<conio.h>
#include<process.h>
#include<stdio.h>
#include<iostream.h>
#define File_Len 10//定义文件名长度
#define Key_Len 20//定义密匙长度
void encrypt()//加密函数
{
char infile[File_Len];
char key[Key_Len];
cout<<"\nInput the filename:";
cin>>infile;
cout<<"Input the key:";
cin>>key;
FILE *fp;
int i=0;
char *p=key,ch;
if((fp=fopen(infile,"r+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(*p=='\0') /*如果密钥长度结束*/
p=key;/*重新赋值*/
if(ch>='a'&&ch<='z')
{
if(*p>='a'&&*p<='z')
ch=((ch-96)+(*p-96))%26+95;
else
ch=((ch-96)+(*p-64))%26+95;
}
else if(ch>='A'&&ch<='Z')
{
if(*p>='A'&&*p<='Z')
ch=((ch-64)+(*p-64))%26+63;
else
ch=((ch-64)+(*p-96))%26+63;
}
else ;
p++,i++;
fseek(fp,i-1,0); /*定位到将要写入字符的位置*/
fputc(ch,fp); /*把加密的字符写入文件中*/
fseek(fp,i,0); /*定位到下一个字符所在的位置*/
ch=fgetc(fp);
}
fclose(fp);
}
/*****************************************************************/
void decrypt()//解密函数
{
char infile[File_Len];
char key[Key_Len];
cout<<"\nInput the filename:";
cin>>infile;
cout<<"Input the key:";
cin>>key;
FILE *fp;
int i=0;
char *p=key,ch;
if((fp=fopen(infile,"r+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(*p=='\0')
p=key;//Loop of key
if(ch>='a'&&ch<='z')
{
if(*p>='a'&&*p<='z')
ch=((ch-96)+26-(*p-96))%26+97;
else
ch=((ch-96)+26-(*p-64))%26+97;
}
else if(ch>='A'&&ch<='Z')
{
if(*p>='A'&&*p<='Z')
ch=((ch-64)+26-(*p-64))%26+65;
else
ch=((ch-64)+26-(*p-96))%26+65;
}
else
p++,i++;
fseek(fp,i-1,0);
fputc(ch,fp);
fseek(fp,i,0);
ch=fgetc(fp);
}
fclose(fp);
}
/*****************************************************************/
void main()//主函数
{
int choice;
Loop:
do
{
//clrscr();
cout<<"Welcome to use!\n";
cout<<"Author:DengJianguo(Icerain)\n";
cout<<"E-mail:djg3412@yahoo.com.cn \n";
cout<<"\nPlease make a choice from fallowing options:\n";
cout<<"0)Exit \n";
cout<<"1)Encrypt \n";
cout<<"2)Decrypt \n";
cout<<"\n Please input a option number[0-2]:";
choice=getchar();
}while(choice!='0'&&choice!='1'&&choice!='2');
if(choice=='0') exit(0);
if(choice=='1')
{
encrypt();
goto Loop;
}
if(choice=='2')
{
decrypt();
goto Loop;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -