鱼C论坛

 找回密码
 立即注册
查看: 1883|回复: 7

[作品展示] 字符串加密与解密

[复制链接]
发表于 2023-12-23 17:21:32 | 显示全部楼层
你的代码里有一些错误。首先,在计算字符串密钥中每个字符的ASCII码和时,应该将字符转换为对应的ASCII码再相加;其次,在判断是否为字母时,你没有返回结果。下面是修正后的代码:
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6. bool isalpha(char ch) {
  7.     if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
  8.         return true;
  9.     } else {
  10.         return false;
  11.     }
  12. }
  13. string encrypt(string str, string key) {
  14.     int sum = 0;
  15.     for (char ch : key) {
  16.         sum += int(ch);
  17.     }
  18.     srand(sum);
  19.     string encryptedStr = "";
  20.     int info1 = rand() % 256;
  21.     int info2 = rand() % 256;
  22.     int info3 = rand() % 256;
  23.     int info4 = rand() % 256;
  24.     srand(info1);
  25.     for (char ch : str) {
  26.         if (isalpha(ch)) {
  27.             int shift = rand() % 26;
  28.             if (islower(ch)) {
  29.                 encryptedStr += char((int(ch) - int('a') + shift) % 26 + int('a'));
  30.             } else {
  31.                 encryptedStr += char((int(ch) - int('A') + shift) % 26 + int('A'));
  32.             }
  33.         } else {
  34.             encryptedStr += ch;
  35.         }
  36.     }
  37.     srand(info2);
  38.     for (int i = 0; i < info3; i++) {
  39.         int index1 = rand() % encryptedStr.length();
  40.         int index2 = rand() % encryptedStr.length();
  41.         swap(encryptedStr[index1], encryptedStr[index2]);
  42.     }
  43.     srand(info4);
  44.     return encryptedStr;
  45. }
  46. string decrypt(string encryptedStr, string key) {
  47.     int sum = 0;
  48.     for (char ch : key) {
  49.         sum += int(ch);
  50.     }
  51.     srand(sum);
  52.     string decryptedStr = "";
  53.     int info1 = rand() % 256;
  54.     int info2 = rand() % 256;
  55.     int info3 = rand() % 256;
  56.     int info4 = rand() % 256;
  57.     srand(info4);
  58.     for (int i = 0; i < info3; i++) {
  59.         int index1 = rand() % encryptedStr.length();
  60.         int index2 = rand() % encryptedStr.length();
  61.         swap(encryptedStr[index1], encryptedStr[index2]);
  62.     }
  63.     srand(info2);
  64.     for (char ch : encryptedStr) {
  65.         if (isalpha(ch)) {
  66.             int shift = rand() % 26;
  67.             if (islower(ch)) {
  68.                 decryptedStr += char((int(ch) - int('a') - shift + 26) % 26 + int('a'));
  69.             } else {
  70.                 decryptedStr += char((int(ch) - int('A') - shift + 26) % 26 + int('A'));
  71.             }
  72.         } else {
  73.             decryptedStr += ch;
  74.         }
  75.     }
  76.     srand(info1);
  77.     return decryptedStr;
  78. }
  79. int main() {
  80.     string str;
  81.     cout << "请输入要加密的字符串:";
  82.     getline(cin, str);
  83.     string key;
  84.     cout << "请输入字符串密钥:";
  85.     getline(cin, key);
  86.     string encryptedStr = encrypt(str, key);
  87.     cout << "加密后的字符串:" << encryptedStr << endl;
  88.     string decryptedStr = decrypt(encryptedStr, key);
  89.     cout << "解密后的字符串:" << decryptedStr << endl;
  90.     return 0;
  91. }
复制代码

这个代码实现了字符串的加密和解密功能。你可以尝试输入一个字符串和一个字符串密钥,程序会输出加密后的字符串和经过解密后的字符串。注意,密钥要与加密时使用的密钥一致才能正确解密。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-23 17:22:26 | 显示全部楼层
陈尚涵 发表于 2023-12-23 17:22
@zhangjinxuan @高山 @学习编程中的Ben

嗨,@zhangjinxuan @高山 @学习编程中的Ben!有什么问题我可以帮忙解答吗?

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-23 17:26:29 | 显示全部楼层
陈尚涵 发表于 2023-12-23 17:25
忘记加上食用过程了,等下

哦,抱歉!让我现在补上食用过程。下面是加密和解密的完整代码示例:
  1. import random
  2. def encrypt(text, key):
  3.     # 计算字符串密钥中每个字符的ASCII码和
  4.     key_sum = sum(ord(ch) for ch in key)
  5.    
  6.     # 从和中提取四个信息
  7.     info_1 = key_sum % 26
  8.     info_2 = (key_sum // 26) % 26
  9.     info_3 = (key_sum // (26**2)) % 26
  10.     info_4 = (key_sum // (26**3)) % 26
  11.    
  12.     # 生成随机数种子
  13.     random.seed(info_1)
  14.    
  15.     # 加密字符串
  16.     encrypted_text = ""
  17.     for ch in text:
  18.         if ch.isalpha():
  19.             shift = random.randint(1, 26)
  20.             if ch.islower():
  21.                 encrypted_ch = chr((ord(ch) - ord('a') + shift) % 26 + ord('a'))
  22.             else:
  23.                 encrypted_ch = chr((ord(ch) - ord('A') + shift) % 26 + ord('A'))
  24.             encrypted_text += encrypted_ch
  25.         else:
  26.             encrypted_text += ch
  27.             
  28.     # 随机数交换值
  29.     random.seed(info_2)
  30.     for i in range(info_3):
  31.         index_1 = random.randint(0, len(encrypted_text)-1)
  32.         index_2 = random.randint(0, len(encrypted_text)-1)
  33.         encrypted_text = list(encrypted_text)
  34.         encrypted_text[index_1], encrypted_text[index_2] = encrypted_text[index_2], encrypted_text[index_1]
  35.         encrypted_text = "".join(encrypted_text)
  36.    
  37.     return encrypted_text
  38. def decrypt(text, key):
  39.     # 计算字符串密钥中每个字符的ASCII码和
  40.     key_sum = sum(ord(ch) for ch in key)
  41.    
  42.     # 从和中提取四个信息
  43.     info_1 = key_sum % 26
  44.     info_2 = (key_sum // 26) % 26
  45.     info_3 = (key_sum // (26**2)) % 26
  46.     info_4 = (key_sum // (26**3)) % 26
  47.    
  48.     # 随机数交换值(反向)
  49.     random.seed(info_2)
  50.     for i in range(info_3):
  51.         index_1 = random.randint(0, len(text)-1)
  52.         index_2 = random.randint(0, len(text)-1)
  53.         text = list(text)
  54.         text[index_1], text[index_2] = text[index_2], text[index_1]
  55.         text = "".join(text)
  56.    
  57.     # 生成随机数种子(反向)
  58.     random.seed(info_1)
  59.    
  60.     # 解密字符串
  61.     decrypted_text = ""
  62.     for ch in text:
  63.         if ch.isalpha():
  64.             shift = random.randint(1, 26)
  65.             if ch.islower():
  66.                 decrypted_ch = chr((ord(ch) - ord('a') - shift) % 26 + ord('a'))
  67.             else:
  68.                 decrypted_ch = chr((ord(ch) - ord('A') - shift) % 26 + ord('A'))
  69.             decrypted_text += decrypted_ch
  70.         else:
  71.             decrypted_text += ch
  72.    
  73.     return decrypted_text
  74. # 测试加密和解密过程
  75. plaintext = "Hello, World!"
  76. key = "secret"
  77. ciphertext = encrypt(plaintext, key)
  78. print("加密后的文本:", ciphertext)
  79. decrypted_text = decrypt(ciphertext, key)
  80. print("解密后的文本:", decrypted_text)
复制代码

希望以上代码可以帮到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-7 05:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表