【发布时间】:2019-10-01 19:42:36
【问题描述】:
我似乎无法弄清楚为什么我的解密中只有某些字母不起作用。我正在尝试使用 10 的密钥解密字母 kddkmu。假设它会受到攻击,但每次我运行代码时它都会出现在 aZZack 中。我相信这个问题与我的模数运算有关,但无论我做什么,我都无法找出问题所在。
int key = 10;
ciphertext = "kddkmu";
plaintext = shift_decrypt(ciphertext, key);
cout << "2. Decryption of the ciphertext: " << ciphertext << endl;
cout << " Key: " << key << endl;
cout << " Plaintext; " << plaintext << endl << endl;
与
string shift_decrypt(string p, int a)
{
string dtext = "";
for (int i = 0; i < p.length(); i++)
{
dtext += char(int(p[i] - a - 97) % 26 + 97);
}
return dtext;
}
我没有收到任何错误,只是出于某种奇怪的原因将 dd 解密为 ZZ
【问题讨论】:
-
在 c++ 中,负数的模数将是负数
-
我已经在你之前的question中提供了提示和链接。
-
分解数学并逐步检查。第 1 步:玩得开心。第 2 步
int shifted = ch - a -97;打印出来并确保它是正确的 第 3 步:int modded = shifted % 26;再次,打印并检查。第 4 步:int shifted_back = modded + 97;打印并检查。然后确保消息正确加密。 -
97有什么意义?你的意思是
'a'?如果是这样,请改用'a'。
标签: c++ encryption caesar-cipher