学了凯撒加密法,自己动手写了个最简单的凯撒加密法的程序。
有兴趣的可以下载玩玩
这是百度网盘的下载地址
下面是源代码
//这是一个凯撒加密程序
//作者 马小李
//2012年10月5日
#define maxlength 50
char const smalllist[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};//小写字母表
#include<iostream>
using namespace std;
#include<fstream>
#include<string.h>
class encryption{
char plaintext[maxlength]; //明文文件名
char ciphertext[maxlength]; //密文文件名
int key; //密钥
public:
void setplaintfile(){cin>>plaintext;strncat(plaintext,".txt",5);} //设置需要加密的文件名
void setchipherfile(){cin>>ciphertext;strncat(ciphertext,".txt",5);} //设置需要解密的文件名
void setkey(){cin>>key;key=key%26;}
void encryptiontext(); //加密文件
void decryptiontext(); //解密文件
char encrypt(char temp); //加密单个字符
char decrypt(char temp); //解密单个字符
};
//加密文件
void encryption::encryptiontext()
{
ifstream infile; //需要加密或解密的文件
ofstream outfile; //输出结果的文件
infile.open(plaintext,ios::in);
if(!infile)//如果打开失败,返回错误
{
cerr<<"open error1!"<<endl;
exit(1);
}
outfile.open("加密文件.txt",ios::out);
if(!outfile)//如果打开失败,返回错误
{
cerr<<"open error2!"<<endl;
exit(1);
}
char getin;//获得输入数据
char code;//加密后数据
getin=infile.get();
while(1){
if(infile.eof()) //文件读取结束
break;
code = encrypt(getin);
outfile<<code;
getin = infile.get();
}
infile.close();
outfile.close();
cout<<"加密文件完成!"<<endl;
}
//解密文件
void encryption::decryptiontext()
{
ifstream infile; //需要加密或解密的文件
ofstream outfile; //输出结果的文件
infile.open(ciphertext,ios::in);
if(!infile)//如果打开失败,返回错误
{
cerr<<"open error1!"<<endl;
exit(1);
}
outfile.open("解密文件.txt",ios::out);
if(!outfile)//如果打开失败,返回错误
{
cerr<<"open error2!"<<endl;
exit(1);
}
char getin;//获得输入数据
char code;//加密后数据
getin=infile.get();
while(1){
if(infile.eof()) //文件读取结束
break;
code = decrypt(getin);
outfile<<code;
getin = infile.get();
}
infile.close();
outfile.close();
cout<<"解密文件完成!"<<endl;
}
//加密单个字符
char encryption::encrypt(char temp)
{
bool bigflag=false;
if((temp>=97&&temp<=122)|(temp>=65&&temp<=90))
{
if(temp>=65&&temp<=90)
{
bigflag=true;
temp=temp+32;
}
temp=temp%97;
if(key>=0)
temp=(temp+key)%26;
else
{
temp=temp+key;
if(temp<0)
temp=temp+26;
}
if(bigflag) return smalllist[temp]-32;
else return smalllist[temp];
}
else
return temp;
}
char encryption::decrypt(char temp)
{
bool bigflag=false;
if((temp>=97&&temp<=122)|(temp>=65&&temp<=90))
{
if(temp>=65&&temp<=90)
{
bigflag=true;
temp=temp+32;
}
temp=temp%97;
if(key<0)
temp=(temp-key)%26;
else
{
temp=temp-key;
if(temp<0)
temp=temp+26;
}
if(bigflag) return smalllist[temp]-32;
else return smalllist[temp];
}
else
return temp;
}
main()
{
encryption user;
while(1){
cout<<"请选择操作:"<<endl;
cout<<"1.加密文件"<<endl<<"2.解密文件"<<endl<<"3.退出"<<endl;
char choice;
cin>>choice;
cin.get();
switch(choice){
case '1' :
system("cls");
cout<<"请输入需要加密的文件名:"<<endl;
user.setplaintfile();
cout<<"请设置密钥:"<<endl;
user.setkey();
cout<<"加密正在进行中……"<<endl;
user.encryptiontext();
system("cls");
break;
case '2':
system("cls");
cout<<"请输入需要解密的文件名:"<<endl;
user.setchipherfile();
cout<<"请输入密钥:"<<endl;
user.setkey();
cout<<"解密正在进行中……"<<endl;
user.decryptiontext();
system("cls");
break;
case '3':
exit(1);
default:
cout<<"没有这个选项,请重新输入"<<endl;
}
}
}