To break the ADFGVX cipher, I've found that you need to find the transposition key first, apply that, and see if it works with monoalphabetic substitution. The transposition key is found through using hillclimbing with IOC and finding which keys give the best IOC. However, the script does not always seem to accurately give the key, even when trying to iterate through the 10 best keys it finds. Have I missed something here, or is there a better way to do this? Here is the function I use to find the key
def HillClimbingTransposition(text, key, bestiocs, bestkeys,iterations):
bestioc = -99
besttext = text
keylength = len(key)
iteration = 0
while iteration < iterations:
iteration += 1
#print(iteration)
newkey = key[:]
RandomNumber1 = (random.randrange(keylength))
RandomNumber2 = (random.randrange(keylength))
a, b = RandomNumber1, RandomNumber2
newkey[a], newkey[b] = newkey[b], newkey[a]
newstring = DecryptTransposition(text, newkey)
ioc = IOC(newstring)
if ioc > bestioc:
bestioc = ioc
key = newkey
besttext = newstring
#print(iteration)
iteration = 0
if ioc > min(bestiocs) and key not in bestkeys:
#besttransposition = bestkey[1]
index = bestiocs.index(min(bestiocs))
bestiocs[index] = ioc
bestkeys[index] = key
#print(bestiocs)
#print(bestkeys)
return (besttext, key, bestiocs, bestkeys)
the full repl link is https://replit.com/@creeperthecat/ADFGVX-Cipher#main.py