编写一个字符串加密类CEncryptInfo, 输入字符串和秘钥串加密(加密函数),加密算法自选(可异或、变位、加减乘除),输出加密串,再用相同的秘钥串解密(解密函数),输出解密串,对外接口函数string encrypt(string strOri, string code); string decrypt(string strEncrypt, string code);
#include <iostream>
using namespace std;
enum Mode
{
SUM,
XOR
};
class CEncryptInfo
{
private:
Mode mode;
public:
CEncryptInfo(Mode m) : mode(m){};
string encrypt(string strOri, string code)
{
string strAim = strOri;
int max = code.length();
int s = 0;
if (mode == SUM)
{
for (int i = 0; i < strAim.length(); i++)
{
strAim[i] = strAim[i] + code[s++];
if (s >= max)
s = 0;
}
return strAim;
}
return "todo";
}
string decrypt(string strEncrypt, string code)
{
string strAim = strEncrypt;
int max = code.length();
int s = 0;
if (mode == SUM)
{
for (int i = 0; i < strAim.length(); i++)
{
strAim[i] = strAim[i] - code[s++];
if (s >= max)
s = 0;
}
return strAim;
}
return "todo";
}
};
int main(int argc, char const *argv[])
{
CEncryptInfo *engine = new CEncryptInfo(SUM);
string code = "test", key = "key";
string encode = engine->encrypt(code, key);
string decode = engine->decrypt(encode, key);
cout << encode << endl
<< decode << endl;
}