Esempio n. 1
0
import crypto

input = crypto.hex_to_str("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")


key, score, string = crypto.break_byte_key_english(input)

print "Key: '{}' ({}): {}".format(chr(key), score, string)
Esempio n. 2
0
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)