【发布时间】:2017-03-15 01:41:54
【问题描述】:
目前我正在尝试在 C++ 中创建一个蛮力凯撒密码,它会在猜测每个密钥时显示所有迭代。
到目前为止,这是我的代码:
#include <iostream>
using namespace std;
int main()
{
char message[100], ch;
int i;
cout << "Enter a message to decrypt: ";
cin.getline(message, 100);
for(int key = 0; key<26 ; key++)
{
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch > 'a'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
cout << "Decrypted message: " << message << " " << endl ;
}
return 0;
}
我遇到的问题是,当它显示猜测时,它似乎在小写字母时跳过字母,而当它们是大写字母时卡住并重复相同的字符串。在示例中,代码是 LT HDGJWJFLQJX,它应该解密为 GO CYBEREAGLES。大写和小写的程序输出都显示在所附的图像中。
【问题讨论】:
-
您首先需要重新格式化您的代码。它没有正确排列。
-
您的文字图片isn't very helpful。它无法大声朗读或复制到编辑器中,并且索引不是很好,这意味着有相同问题的其他用户不太可能在这里找到答案。请edit您的帖子直接合并相关文本(最好使用复制+粘贴以避免转录错误)。
标签: c++ encryption cryptography caesar-cipher