In school we only learned the formula without the normalizing coefficient:

enter image description here

But Wikipedia has one that says:

enter image description here

I am using IC in a cryptography class to find the key length and am checking various key lengths to see which one results in an IC closest to 0.066. I have to automate the key length finding process but I don't know which formula I should use. What does the normalizing coefficient change about the value? Does it make it better for finding the key length? Which formula should I use for my case?

from freq_analysis import freqs
import string

def ioc(fname, normalize: bool = False):
    frq = list(freqs(string.ascii_uppercase, fname).values())
    n = sum(frq)
    for x in range(0, len(frq)):
        frq[x] = frq[x] * (frq[x] - 1)
    frq = sum(frq)
    if normalize:
        return frq*26 / (n*(n-1))
        return frq / (n*(n-1))

Full context for above code here: https://github.com/DarkFireGuy/cracking-vigenere (if you want to see context for freq_analysis and freqs())


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.