import crypto input = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal" print crypto.str_to_hex(crypto.xorstring_key(input, "ICE"))
import crypto best = "" bestkey = "" bestscore = 0.0 with open('data/4.txt', 'r') as f: for line in f: ct = crypto.hex_to_str(line.rstrip()) for k in range(256): s = crypto.xorstring_key(ct, chr(k)) score = crypto.score_english(s) if score > bestscore: best = s bestkey = k bestscore = score print "Key: '{}' ({}): {}".format(chr(bestkey), bestscore, best)
import crypto # Sanity test for 'get_hamming_distance' s1 = "this is a test" s2 = "wokka wokka!!!" assert crypto.get_hamming_distance(s1, s2) == 37 ct = crypto.base64_to_str(open('data/6.txt', 'r').read()) # Try to guess the keysize based on the keysize that yields the minimum hamming # distance keysize = min(range(2, min(len(ct)/4, 41)), key=lambda ks: crypto.get_normalized_hamming_distance(ct, ks, n=4)) # Now break up the ciphertext into blocks on length 'keysize' and transpose so # that we can do frequency analysis on the result. blocks = crypto.transpose_str(ct, keysize) key = "" for block in blocks: keybyte, _, _ = crypto.break_byte_key_english(block) key += chr(keybyte) print "Key: '{}'".format(key) print crypto.xorstring_key(ct, key)