鱼C论坛

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

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

[复制链接]
发表于 2023-12-23 17:21:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 陈尚涵 于 2023-12-23 17:28 编辑

今天下午闲的没事 玩原神 研究字符串加密
那么来看一下加密过程吧
1.需要输入一个要加密的字符串和字符串密钥
2.计算字符串密钥中每一个字符的ascii码的和
3.从和中提取四个信息
4.用其中一个信息作为种子,对于字符串的每一个大小写字母,都增加不同的值,这里使用随机数,这个过程重复另一个信息次
5.用其中一个信息作为种子,通过随机数交换两个数的值,这个过程重复另一个信息次
6.结束
然后是解密,跟加密过程反着来就可以了,主要是要先用数组提取随机数,再倒过来使用随机数,所以速度比加密慢一倍
好吧,过程确实很简单,但是我这个蒟蒻花了两个半小时从构思到写代码
ps.建议不要使用汉字或其他语言,应使用英语,以免发生奇奇怪怪的bug
然后就是食用过程
比如说要加密
  1. hello world!
复制代码

我们还需要一个密钥,比如可以是网名,比如我的网名就是
  1. asionking
复制代码

那么运行加密,输入
  1. hello world!
  2. asionking
复制代码

得到
  1. fc!nkkd epzz
复制代码

这个就是加密后的了
接下来解密,一样,输入
  1. fc!nkkd epzz
  2. asionking
复制代码

就可以解出来了
  1. hello world!
复制代码

那么上代码
加密代码:
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. using namespace std;
  5. bool isalpha(char ch){
  6.         if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
  7.                 return true;
  8.         }
  9.         return false;
  10. }
  11. char change(char str[], int len, int seed, int ts){
  12.         srand(seed);
  13.         for (int i = 1; i <= ts; i++){
  14.                 for (int j = 0; j < len; j++){
  15.                         if (!isalpha(str[j])){
  16.                                 continue;
  17.                         }
  18.                         int x = rand()%26;
  19.                         for (int k = 1; k <= x; k++){
  20.                                 if (str[j]=='z'||str[j]=='Z'){
  21.                                         str[j]-='z'-'a';
  22.                                 } else {
  23.                                         str[j]++;
  24.                                 }
  25.                         }
  26.                 }
  27.         }
  28. }
  29. void move(char str[], int len, int seed, int ts){
  30.         srand(seed);
  31.         for (int i = 1; i <= ts; i++){
  32.                 int x = rand()%len;
  33.                 int y = rand()%len;
  34.                 char ch = str[x];
  35.                 str[x] = str[y];
  36.                 str[y]= ch;
  37.         }
  38. }
  39. int sigma(char str[], int len){
  40.         int sum = 0;
  41.         for(int i = 0; i < len; i++){
  42.                 sum += str[i];
  43.         }
  44.         return sum;
  45. }
  46. int main(){
  47.         char str[666];
  48.         char key[666];
  49.         cin.getline(str,666);
  50.         cin.getline(key,666);
  51.         int len = strlen(str);
  52.         int keylen = strlen(key);
  53.         int sum = sigma(key, keylen);
  54.         int movets = sum / 26;
  55.         int changets = sum % 26;
  56.         int moveseed = sum - movets;
  57.         int changeseed = sum - changets;
  58.         change(str, len, changeseed, changets);
  59.         move(str, len, moveseed, movets);
  60.         cout << str;
  61.         return 0;
  62. }
复制代码

解密代码
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. using namespace std;
  5. bool isalpha(char ch){
  6.         if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
  7.                 return true;
  8.         }
  9.         return false;
  10. }
  11. char change(char str[], int len, int seed, int ts){
  12.         srand(seed);
  13.         int xr[114514];
  14.         int cnt = 0;
  15.         int isalphats = 0;
  16.         for (int i = 1; i <= ts; i++){
  17.                 for (int j = len - 1; j >= 0; j--){
  18.                         if (isalpha(str[j])){
  19.                                 isalphats++;
  20.                         }
  21.                 }
  22.         }
  23.         for (int i = 1; i <= isalphats; i++){
  24.                 xr[cnt++] = rand();
  25.         }
  26.         int now = cnt - 1;
  27.         for (int i = 1; i <= ts; i++){
  28.                 for (int j = len - 1; j >= 0; j--){
  29.                         if (!isalpha(str[j])){
  30.                                 continue;
  31.                         }
  32.                         int x = xr[now--]%26;
  33.                         for (int k = 1; k <= 26-x; k++){
  34.                                 if (str[j]=='z'||str[j]=='Z'){
  35.                                         str[j]-='z'-'a';
  36.                                 } else {
  37.                                         str[j]++;
  38.                                 }
  39.                         }
  40.                 }
  41.         }
  42. }
  43. void move(char str[], int len, int seed, int ts){
  44.         int xr[114514];
  45.         int yr[114514];
  46.         int cnt = 0;
  47.         srand(seed);
  48.         for (int i = 1; i <= ts; i++){
  49.                 xr[cnt] = rand()%len;
  50.                 yr[cnt] = rand()%len;
  51.                 cnt++;
  52.         }
  53.         int now = cnt - 1;
  54.         for (int i = 1; i <= ts; i++){
  55.                 int x = xr[now];
  56.                 int y = yr[now];
  57.                 now--;
  58.                 char ch = str[x];
  59.                 str[x] = str[y];
  60.                 str[y]= ch;
  61.         }
  62. }
  63. int sigma(char str[], int len){
  64.         int sum = 0;
  65.         for(int i = 0; i < len; i++){
  66.                 sum += str[i];
  67.         }
  68.         return sum;
  69. }
  70. int main(){
  71.         char str[666];
  72.         char key[666];
  73.         cin.getline(str,666);
  74.         cin.getline(key,666);
  75.         int len = strlen(str);
  76.         int keylen = strlen(key);
  77.         int sum = sigma(key, keylen);
  78.         int movets = sum / 26;
  79.         int changets = sum % 26;
  80.         int moveseed = sum - movets;
  81.         int changeseed = sum - changets;
  82.         move(str, len, moveseed, movets);
  83.         change(str, len, changeseed, changets);
  84.         cout << str;
  85.         return 0;
  86. }
复制代码

喜欢的话可以给我评分吗

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
学习编程中的Ben + 5 + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 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:17 | 显示全部楼层
小甲鱼最新课程 -> 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
回复 支持 反对

使用道具 举报

发表于 2023-12-23 18:03:58 | 显示全部楼层
楼上挺6
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-23 18:08:05 | 显示全部楼层
建议使用 mt19937,不同环境下、同种子的 rand 的结果可能不一样。

手写随机函数也是个不错的选择。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-23 18:37:35 | 显示全部楼层
zhangjinxuan 发表于 2023-12-23 18:08
建议使用 mt19937,不同环境下、同种子的 rand 的结果可能不一样。

手写随机函数也是个不错的选择。

感谢建议
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-19 10:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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