有一次帮一个同学看程序。
需求是:有映射加密和移位加密两种方式,对in.txt文件中的小写字母进行双重加密,输出为out.txt。要求用面向对象的方式来写,有加密器类、映射加密器类、移位加密器类等。
程序一共有5个文件,第一个是main.cpp代表主程序,第二个是paraMapTxt.dat代表映射加密的参数文件,第三个是paraShiftTxt.dat代表移位加密的参数文件。第四个是In.txt代表输入,第五个是out.txt代表输出文件。这5个文件要放在同一个目录下。对main.cpp进行编译和运行即可进入系统。
①main.cpp
/*
Author: wuki HUST
Date: 24 June 2021
Email: sxdxlwq@qq.com
Function: Double Encryption for given strings (Map Encryption || Shift Encryption)
*/
#include <iostream>
#include <string>
#include <cstdio>
#include <stdlib.h>
#include <map>
#include <fstream>
using namespace std;
/* 加密容器类 */
class Enc {
public:
string plainText;
string cipherText;
Enc(){}
~Enc(){}
string getPlain() {return plainText;}
string getCipher() {return cipherText;}
void setCipher(string s) {cipherText = s;}
virtual string encrypt(string){}
virtual string decrypt(string){}
//virtual void changeCipher(string);
};
/* 映射加密容器类 */
class MapEnc:public Enc {
public:
map<char, char> mp;
MapEnc(){}
~MapEnc(){}
void changeMap(string s) {
for(int i = 0; i < 26; i++) {
mp['a' + i] = s[i];
}
}
string encrypt(string plain) {
string tmp = plain;
for (int i = 0; i < plain.size(); i++) {
if(plain[i] >= 'a' && plain[i] <= 'z') tmp[i] = mp[plain[i]];
}
return tmp;
}
string decrypt(string cipher){
//先翻转mp,然后调用encrypt函数,然后从再翻转mp,然后return值
map<char, char> tmp = mp;
for (int i = 0; i < 26; i++) {
mp[tmp['a' + i]] = 'a' + i;
}
string s = encrypt(cipher);
mp = tmp;
return s;
}
/*
void changeCipher() {
string pla = getPlain();
for (int i = 0; i < pla.size(); i++) {
pla[i] = mp[pla[i]];
}
setCipher(pla);
}
*/
};
/* 移位加密容器类 */
class ShiftEnc:public Enc {
public:
int base;
ShiftEnc(){}
~ShiftEnc(){}
void changebase(int x){
base = x;
}
string encrypt(string plain) {
string tmp = plain;
for (int i = 0; i < plain.size(); i++) {
if(plain[i] >= 'a' && plain[i] <= 'z') tmp[i] = 'a' + ((plain[i] - 'a' + ba