1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| #include <iostream> #include <stdio.h> #include <cstring> #include <regex> using namespace std;
regex reg_aTOz("^[a-z]$"); regex reg_AtoZ("^[A-Z]$"); regex reg_num("^[0-9]$"); regex reg_lb("^[\u4e00-\u9fa5]+$");
void ui() { cout << "\t\t\t—————————————— 循环移位密码 Caesar Normal Stu ——————————————" << endl; cout << "" << endl; }
char encryptionOrDecryptionChanges(char originalChar, char singleChar, int key, int isDecryption = 0) { key = abs(key); string singleStr(1,singleChar); if (!isalnum(originalChar)) { if((singleChar >= 32 && singleChar <= 47) || (singleChar >= 58 && singleChar <= 64)) return singleChar + 40; if(singleChar >= 91 && singleChar <= 96) return singleChar + 15; if(singleChar >= 123 && singleChar <= 126) return singleChar - 10; if((singleChar >= 72 && singleChar <= 87) || (singleChar >= 98 && singleChar <= 104)) return singleChar - 40; if(singleChar >= 106 && singleChar <= 111) return singleChar - 15; if(singleChar >= 113 && singleChar <= 116) return singleChar + 10; } if (regex_match(singleStr, reg_AtoZ)) { if(isDecryption != 0) return 'A' + (singleChar - 'A' - (key % 26) + 26) % 26; return 'A' + (singleChar - 'A' + (key % 26)) % 26; } else if (regex_match(singleStr, reg_aTOz)) { if(isDecryption != 0) return 'a' + (singleChar - 'a' - (key % 26) + 26) % 26; return 'a' + (singleChar - 'a' + (key % 26)) % 26; } else if (regex_match(singleStr, reg_num)) { if(isDecryption != 0) return '0' + (singleChar - '0' - (key % 10) + 10) % 10; return '0' + (singleChar - '0' + (key % 10)) % 10; }
return ' '; }
void caesar() { char plaintext[1000] = {}; char ciphertext[1000] = {}; int i, len, encryptionKey, decryptionKey;
ui();
while (true) { cout << "输入明文(<1000 个字符):"; cin >> plaintext;
if(strlen(plaintext) < 1000) break; cout << "当前长度为:" << strlen(plaintext) << " 字符最大长度为:999,请重新输入" << endl; } len = strlen(plaintext); char* pointerPlaintext = plaintext; cout << "查看输入的明文:"; for (i = 0; i < len; i++) cout << *pointerPlaintext++;
cout << endl << "开启加密,输入密钥(任意数):"; cin >> encryptionKey;
for (i = 0; i < len; i++) ciphertext[i] = encryptionOrDecryptionChanges(plaintext[i], plaintext[i], encryptionKey);
cout << "完成加密,密文为:"; for (i = 0; i < len; i++) cout << ciphertext[i];
cout << endl;
char mingWen[1000] = {};
cout << "进行解密,输入密钥(需要和加密密钥一致):"; cin >> decryptionKey;
if (encryptionKey == decryptionKey) { for (i = 0; i < len; i++) mingWen[i] = encryptionOrDecryptionChanges(plaintext[i], ciphertext[i], decryptionKey, 1);
string strP = plaintext, strM = mingWen; if (strP.compare(strM) == 0) { cout << "??????? 完成解密,明文为:"; for (i = 0; i < len; i++) cout << mingWen[i]; } else cout << "解密失败,因解密后明文与输入时明文不一致!!"; cout << endl; } else cout << "加解密密钥不一致,解密失败!" << endl; }
int main() { while (1) caesar(); return 0; }
|