Trying to decrypt substitution using letter frequencies

Viewed 49

In order to decrypt the two-step cipher involving substitution and columnar transposition, I decided to try and tackle this by comparing the letter frequencies in the ciphertext with English frequencies for substitution and use tetragram fitness for transposition. The transposition part seems to work fine, but the code doesn't seem to work very well with the substitution part. Does anyone have any ideas as to how to fix this, or a different solution entirely? If you want the full repl file the link is https://replit.com/@creeperthecat/substitution-then-columnar-transposition#main.py

def FrequencySubstitution(text):
    frequencies = LetterFrequencies(text) #this function gives a float between 0 and 1 for each letter in the alphabet based on its frequency
    newfrequencies = sorted(frequencies)

    indexes = []

    for i in frequencies:
        count = newfrequencies.count(i)
        if count == 1:
            index = newfrequencies.index(i)
            indexes.append(index)
        else:
            for j in range(count):
                index = newfrequencies.index(i)+j
                if index not in indexes:
                  indexes.append(index)
    indexes.reverse()
    substitutionkey = []
    for i in indexes:
        substitutionkey.append(mostcommonletters[I])#mostcommonletters is a list of English letters in order of the most frequent ones - I realise that this list can vary but it should give enough information to decrypt it properly

    newtext = DecryptSubstitution(text, substitutionkey)

    return (newtext,substitutionkey, indexes)
1 Answers

Turns out this seems to be where current methodology lies. Either the transposition key must be short enough to use brute force, or you have to hope that the letter frequencies are good enough to decrypt with transposition.

Related