【问题标题】:C++ adding spaces to a string of wordsC ++在字符串中添加空格
【发布时间】: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


【解决方案1】:

我能想到的唯一解决方案是使用某种英语词典,遍历每个字符,直到它看到一个单词,然后在其中放置一个空格。例如,此解决方案中的问题可能是 AND 之类的单词,它可以是 a 或 an 或 and。所以没有办法确定在哪里放置空格。也许是一个非常复杂的 AI 算法,它会根据之前看到的单词来确定接下来应该出现的单词。

【讨论】:

  • 这就是我的想法。手动输入常用词并让它在文本中找到它们。该项目不够大,无法在其中实现任何 AI,但无论如何感谢
猜你喜欢
  • 2013-12-21
  • 1970-01-01
  • 2021-06-08
  • 2020-09-16
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
相关资源
最近更新 更多