我正在尝试实现一种算法,该算法将猜测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,则为1,3,5,7,9,但不是2,4,6,8,10 ......)
答案 0 :(得分: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
}