0

我正在尝试实现一种算法,该算法将猜测 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();
    }
}
}

就像我在下面提到的那样,我的输出仅反映基于第一个字符的子字符串(即,如果 keylength 为 2,则为 1、3、5、7、9,但不是 2、4、6、8 , 10...)

4

1 回答 1

0

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

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

}
于 2013-08-13T08:16:11.060 回答