古典密码之移位密码Cpp源码
移位密码又称为移位代换密码,是单表代换密码的一种。
#include <iostream>
#include <fstream>
#include <cstdlib>
/* run this program using the console pauser&nbs***bsp;add your own getch, system("pause")&nbs***bsp;input loop */
using namespace std;
void encryption(ifstream& fin, ofstream& fout, int n);
void decryption(ifstream& fin, ofstream& fout, int n);
int main(int argc, char** argv) {
// 加密
ifstream fin;
ofstream fout;
fin.open("file1_2.in");
if(fin.fail())
{
cout<<"File open error!(Input)"<< endl;
exit (1) ;
}
fout.open("file1_2.out");
if(fout.fail()){
cout<<"File open error!(Outpot)"<< endl;
}
encryption(fin, fout, 20);
fin.close();
fout.close();
// 解密
ifstream fin_de;
ofstream fout_de;
fin_de.open("file1_2.out");
if(fin_de.fail())
{
cout<<"File open error!(Input)"<< endl;
exit (1) ;
}
fout_de.open("file1_2_de.out");
if(fout_de.fail()){
cout<<"File open error!(Outpot)"<< endl;
}
decryption(fin_de, fout_de, 20);
fin_de.close();
fout_de.close();
return 0;
}
void encryption(ifstream& fin, ofstream& fout, int n){
char next;
while(fin.get(next)){
fout<<char((int(next)+n)%128);
}
}
void decryption(ifstream& fin, ofstream& fout, int n){
char next;
while(fin.get(next)){
fout<<char((int(next)-n)%128);
}
}
360集团公司氛围 369人发布
