【发布时间】:2020-02-10 18:30:36
【问题描述】:
例如我有一个密文代码"KWSVVSYXKSBOKRKBNRKDKXNKNBEXUKBOKDKLKBGRO",我对其进行了一些频率分析计算并开始逆向工程来破解密码。
我解决的密文是
“新解决的密文密钥为:AMILLIONAIREAHARDHATANDADRUNKAREATABARWHENT”
现在唯一的问题是我不明白如何正确地在单词之间放置空格,所以可以多一点英文"A millionaire a hard hat and drunk are..."
下面是我破解密码的代码
string cipher = "";
int i = 0, alphabet[26] = { 0 }, j, temp;
int n = cipher.length();
// declaring character array
char char_array[343];
// copying the contents of the
// string to char array
strcpy_s(char_array, cipher.c_str());
//print the entire cipher text and ASCII value
//for (int i = 0; i < n; i++)
//{
//cout << char_array[i] << endl;
// cout << "the ASCII value of " << char_array[i] << " is " << int(char_array[i]) << endl;
// }
//Find the most frequent letter in the cipher text
while (char_array[i] != '\0') {
if (char_array[i] >= 'A' && char_array[i] <= 'Z') {
j = char_array[i] - 'A';
++alphabet[j];
}
++i;
}
cout << "Frequency of all alphabets in the string is:" << endl;
for (i = 0; i < 26; i++)
cout << char(i + 'A') << " : " << alphabet[i] << endl;
//end most frequent
cout << endl;
const int N = sizeof(alphabet) / sizeof(int);
for (i = 0; i < 26; i++){
cout << "Most frequent letter is : " << char(distance(alphabet, max_element(alphabet, alphabet + N)) + 'A') << " trying key : " << distance(alphabet, max_element(alphabet, alphabet + N)) << endl;
//cout << alphabet[i] << "Most frequent position of key is trying: " << distance(alphabet, max_element(alphabet, alphabet + N)) << endl;
cout << "New solved cipherr text with key is : " << encrypt(char_array, distance(alphabet, max_element(alphabet, alphabet + N))) << endl << endl;
alphabet[distance(alphabet, max_element(alphabet, alphabet + N))] = 0;
}
string encrypt(string text, int s)
{
string result = "";
// traverse text
for (int i = 0; i<text.length(); i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
result += char(int(text[i] + s - 65) % 26 + 65);
}
// Return the resulting string
return result;
}
【问题讨论】:
-
您想要一个算法将没有空格的文本拆分成单词?
-
在编码中,除了大写 A 到 Z 之外,您会忽略任何内容。您无法解码在编码步骤中丢弃的信息。
-
避免像
65这样的神奇数字,改用'A'。 (顺便说一句,A-Z 范围不保证是连续的) -
听起来您还需要对空格进行编码。您需要在可能的替换中添加第 27 个字符。这对于 ASCII 字符的数学运算很困难,但如果您创建自己的替换字符数组则很容易......
-
@jarod42,没有。使用空格分割文本
标签: c++ encryption block-cipher letter-spacing