I'm trying to implement an algorithm which will guess at a likely key length of a keyword of a vigenere cipher.
I'm going about the steps of finding the index of coincidence for each possible key length, but I can't figure out a way to split the cipher text into the substrings.
That is, I'm trying to take a certain cipher text like this
ERTEQSDFPQKCJAORIJARTARTAAFIHGNAPROEOHAGJEOIHJA
(this is random text, there's no coded message here)
and split it up into different strings like this:
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)
and so on.
Any ideas?
UPDATE
I've tried implementing my own code now, and here's what I've done:
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();
}
}
}
Like I've mentioned below, I'll have output which only reflects the substring that is based on the first characters (i.e. 1, 3, 5, 7, 9 if the keylength is 2, but not 2, 4, 6, 8, 10...)