#python program to solve a null cipher. #by searching letter patterns # This program outputs all trial decrypts #that contain the crib. # Assume ciphertext has no punctuation print "Search null cipher for crib matches." filename = raw_input("Enter filename: ") f1 = open(filename) codetext = f1.read() f1.close print codetext codetext= codetext.lower() crib = raw_input("Enter crib: ") crib = crib.lower() code = codetext.split() lens = [len(w) for w in code] max_len = max(lens) print "Maximum word length is ",max_len kl = raw_input("Enter pattern length: ") key_len = int(kl) me="count from (F)irst or (L)ast letter: " di = raw_input(me) if di.upper()[0] == 'L': dx = 1 else: dx = 0 key = [0]*key_len def get_plain(): index = 0 plain = '' for w in code: n = key[index] if n>= len(w): return ' ' if dx : plain += w[-n-1] else: plain += w[n] index += 1 if index == key_len: index = 0 return plain def make_key(index): if index == key_len-1: for k in range(max_len): key[index] = k plain = get_plain() if plain.find(crib) > -1 : print plain, key else: for k in range(max_len): key[index] = k make_key(index+1) make_key(0)