#include #include using namespace std;

const string key = "4962873"; // 加密密钥

string encrypt(string s) { // 加密函数 string res = ""; int k = 0; for (char c : s) { int code = c; code += key[k] - '0'; // 反复加上密钥中的数字 code = (code - 32) % 91 + 32; // 取模运算 res += (char)code; k = (k + 1) % key.size(); } return res; }

string decrypt(string s) { // 解密函数 string res = ""; int k = 0; for (char c : s) { int code = c; code = (code - 32 + 91) % 91 + 32; // 取模运算 code -= key[k] - '0'; // 反复减去密钥中的数字 res += (char)code; k = (k + 1) % key.size(); } return res; }

int main() { string s; cout << "请输入一行字符:"; getline(cin, s); // 读取整行字符串 string e = encrypt(s); // 加密 cout << "加密结果:" << e << endl; string d = decrypt(e); // 解密 if (d == s) { // 比较解密结果与输入正文 cout << "解密结果:" << d << endl; } else { cout << "解密失败" << endl; } return 0;

编写程序调试通过然后提交cpp文件。编制程序将输人的一行字符以加密的形式输出然后将其解密解密的字符序列与输入的正文比较吻合时输出解密的正文否则输出解密失败。加密时将每个字符的Asci 码依次反复加上4962873中的数字并在32-122之间做模运算。解密与加密的顺序相反例如对于输入正文the result of3 and 2 is not 8加密结果xqkzlvyuzwm#7gpl5$ryvvw$

原文地址: http://www.cveoy.top/t/topic/chAh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录