I'm trying to write a Vigenere decipher script. I defined a couple of possible keylengths using the Kasiski method, using roughly this method:
- find recurring strings in the ciphertext between 3 and 12 characters
- make a list of the distances between them
- loop through the list of distances and calculate the greatest common denominator between each distance[n] and distance[n+1]
- count the GCDs and send the top 3 to the Friedman function
In the Friedman function:
For each of the GCDs (= possible keylengths) that my Kasiski function returns I calculate the IC, but sometimes a multiple of the keylength is closer to the standard English IC than the actual keylength. This is usually a difference of only 1-3%. Since I do not weigh them by the amount of times the keylength came out in the GCDs counter, it sometimes happens that for a key of 3 characters my function returns 6 as the most likely keylength.
My question is, should I:
- Preserve the amount of times a GCD was established between two distances in the Kasiski function, weighing the outcome of the Friedman function with it to prefer the most occurring GCD
- In the Friedman function, check if a possible keylength is a multiple of another possible keylength, comparing the ICs and discarding the multiple if the difference is (for example) less than a few %
- Both, neither, anything else?