I'm writing a C++ program to use the Kasiski method to crack the Vigenère cipher. I'm testing it with a bit of Shakespeare encrypted with the key ABCDEFGHIJKL. It can find the key length, (even when I changed it), but when I started using frequency analysis, the poem's frequency doesn't seem to fit with normal english; in some lines, T occurs twice as much as E when I try to do frequency analysis.

cout << "The key length is most likely " << len << "." << endl;

for (int i = 0; i < len; i ++) {
    string m;
    int j = 0;
    do {
        m += ciphertext[i + len * j];
    } while (i + len * j++ < length(ciphertext));
    cout << m << endl;

So I input


and it finds the length 12 and it gets the 1st, 13th, 25th, etc. characters, and it correctly gives


But when I analyze the string separately, there are almost twice as more Us (correspond to T) than Fs (correspond to E). I think I did something wrong here…


put on hold as off-topic by kelalaka, Squeamish Ossifrage, AleksanderRas, e-sushi, Maeher 17 hours ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions are off-topic even if you are writing or debugging cryptographic code. Unless your question is specifically about how the cryptographic algorithm, protocol or side-channel (mitigation) works, you should look into asking on Stack Overflow instead." – kelalaka, Squeamish Ossifrage, AleksanderRas, e-sushi, Maeher
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Maybe just run the frequency analysis on the actual text? It is possible, that this part of text does not fit the standard language distribution (don't forget, it takes every 12th symbol). And did you encounter similar problems with the other ensembles of characters (chars at 2,14,26,... ; 3,15,27,...;...)? $\endgroup$ – tylo Jun 9 '15 at 11:57
  • $\begingroup$ @tylo It's not only that part of text. Out of the 11 sequences generated from the square, 4-5 of them don't have the right frequencies... $\endgroup$ – raumaan kidwai Jun 9 '15 at 12:01
  • $\begingroup$ Here goes testing with key length 15... $\endgroup$ – raumaan kidwai Jun 9 '15 at 12:02
  • 2
    $\begingroup$ For this amount of text that's entirely possible. The larger the text, the better the statistics (unless it is off on purpose, e.g. poems could do that). $\endgroup$ – Maarten Bodewes Jun 9 '15 at 18:29
  • 2
    $\begingroup$ Since no one is talking like Shakespeare today, there are differences in language characteristics. For example, in his works the word thou (and its different cases, e.g. thee, thy) is used quite a lot, but in any modern english this would be you instead => leads to an increase in t and a decrease in y $\endgroup$ – tylo Jun 10 '15 at 11:24