#Phillips crib dragging #subroutines def get_cipher(): file = raw_input("Cipher name: ") fid = open(file) ciphertext = fid.read() fid.close() return ciphertext def convert_string(text): chars = "ABCDEFGHIKLM" chars += "NOPQRSTUVWXYZ" text = text.upper() list = [chars.index(c) for c in text if c in chars] return list period = 8 en_alphabet = [None]*period de_alphabet = [None]*period def ck_alphabets(x,y,depth): for n in range(period): c = de_alphabet[n][x] if c == y: return 1 #no good if c != -1 and depth != 2: r = ck_alphabets(c,y,depth+1) if r : return 1#no good return 0 # OK! def do_test(cipher,crib,pos): for n in range(period): en_alphabet[n]= [-1]*25 de_alphabet[n]= [-1]*25 index = (pos/5)%period count = pos % 5 for i in range(len(crib)): c = cipher[i+pos] c1 = en_alphabet[index][crib[i]] c2 = de_alphabet[index][c] if c1 != -1 and c1 != c: return 0 if c2 != -1 and c2 != crib[i]: return 0 en_alphabet[index][crib[i]]=c de_alphabet[index][c] = crib[i] count += 1 if count == 5: count = 0 index = (index+1)%period for i in range(25): #4->0 7->1 c1 = de_alphabet[0][i] c2 = de_alphabet[4][i] if c1 !=-1 and c2!=-1 and c1!=c2: return 0 if c2 != -1: de_alphabet[0][i]=c2 c1 = de_alphabet[1][i] c2 = de_alphabet[7][i] if c1 !=-1 and c2!=-1 and c1!=c2: return 0 if c2 != -1: de_alphabet[1][i]=c2 for n in range(period-1): if n==4: continue for i in range(25): c = de_alphabet[n][i] c1 = c count = 0 while c != -1: c = de_alphabet[n][c] count += 1 if c == c1: #got chain if count == 5: break return 0 #letter changed its row/column? for n in range(period): for y in range(25): c = de_alphabet[n][y] if c != -1: r = ck_alphabets(c,y,0) if r: return 2 #RC maybe OK return 1 #passed all tests #main program print "Phillips crib drag" text = get_cipher() print "Cipher is:" print text cipher = convert_string(text) crib = raw_input("Enter crib: ") crib = convert_string(crib) le = len(cipher)-len(crib)+1 print "Crib OK at positions: ", for pos in range(le): r = do_test(cipher,crib,pos) if r==1: print pos+1, elif r ==2: print "(RC only->",pos+1,")", print