将密文分成子串(vigenere密码)

splitting up cipher text into substrings (vigenere cipher)

本文关键字:vigenere 密码 子串 密文      更新时间:2023-10-16

我正在尝试实现一种算法,该算法将猜测vigenere密码的关键字的可能密钥长度。

我正在讨论为每个可能的密钥长度找到一致性索引的步骤,但我无法找到将密文拆分为子字符串的方法。

也就是说,我正试图获取像这样的特定密文

ERTEQSDFPQKCJAORIJARTARTAAFIHGNAPROEOHAGJEOIHJA(这是随机文本,这里没有编码消息)

并将其分成不同的字符串,如下所示:

key length 2: ETQDP... (every second letter starting from position 0)
              RESFQ... (every second letter starting from position 1)
key length 3: EEDQ.... (every third letter starting from position 0)

等等

有什么想法吗?

更新

我现在已经尝试实现我自己的代码,下面是我所做的:

void findKeyLength(string textToTest)
{
size_t length = textToTest.length();
vector<char> vectorTextChar;
    //keeping key length to half the size of ciphertext; should be reasonable
for (size_t keylength = 1; keylength < length / 2; keylength++)
{
    for (size_t i = keylength; i < keylength ; i++)
    {   
        string subString = "";
        for (size_t k = i; k < length; k+=i)
        {
            vectorTextChar.push_back(textToTest[k]);
        }
        for (vector<char>::iterator it= vectorTextChar.begin(); it!=vectorTextChar.end(); ++it)
        {
            subString += *it;
        }
        cout << subString << endl; //just to see what it looks like
        cout << "Key Length : " << keylength << "IC: " << indexOfCoincidence(subString) << endl;
        vectorTextChar.clear();
    }
}
}

正如我在下面提到的,我的输出只反映基于在第一个字符上(即,如果密钥长度为2,而不是2、4、6、8、10…,则为1、3、5、7、9)

未测试,但您可以执行以下操作:

int len = cipherText.length;
char[] text2inspect = char[(len/keyLength) + 1]
for (int startIndex = 0; keyLength > startIndex; startIndex++){
  int destIndex = 0;
  for (int index = startIndex; len > index; index += keyLength){
    text2inspect[destIndex++] = cipherText[index];
  }
  text2inspect[destIndex] = 0; // String termination
  // add your inspection code here
}