def c7(): KEY = 'YELLOW SUBMARINE' with open('inputs/7.txt') as f: cipher = f.read() cipher = crypto.base64_to_bytes(cipher) key = crypto.str_to_bytes(KEY) plain = crypto.decrypt_AES_ECB(cipher, key) print('Plain: {0}'.format(plain.decode('utf-8')))
def c10(): INPUT_KEY = 'YELLOW SUBMARINE' with open('inputs/10.txt') as f: cipher = f.read() cipher = crypto.base64_to_bytes(cipher) key = crypto.str_to_bytes(INPUT_KEY) plain = crypto.decrypt_AES_CBC(cipher, key) print(plain)
def c6(): HAMMING_INPUTS = ('this is a test','wokka wokka!!!') distance = textutils.hamming_distance(*map(crypto.str_to_bytes, HAMMING_INPUTS)) expect(distance, 37) with open('inputs/6.txt') as f: cipher = f.read() cipher = crypto.base64_to_bytes(cipher) keysize = crypto.brute_xor_keysize(cipher) print('Guessing the key size is {0}'.format(keysize)) key = crypto.brute_xor_key(cipher, keysize) print('Key: {0}'.format(key)) plain = crypto.fixed_xor(cipher, crypto.str_to_bytes(key)) print ('Plain: {0}'.format(plain.decode('utf-8')))
def c10(): # My own test data message = 'The crow flies from the chicken coop at dawn.' key = b'YeLlOw SuBmArInE' blocksize = len(key) iv = bytes([randrange(0, 256) for i in range(0, blocksize)]) cryptbytes = crypto.encrypt_aes_cbc(crypto.str_to_bytes(message), iv, key) print('Encrypted: %r' % cryptbytes) plainbytes = crypto.decrypt_aes_cbc(cryptbytes, iv, key) print('Decrypted: %r' % plainbytes.decode()) expect(plainbytes.decode(), message) # Decrypt the example from Cryptopals key = b'YELLOW SUBMARINE' blocksize = len(key) iv = bytes([0] * blocksize) cryptbytes = crypto.base64_to_bytes(open('inputs/10.txt').read()) decrypted = crypto.decrypt_aes_cbc(cryptbytes, iv, key) print(decrypted.decode())
def c6(): b64text = open('inputs/6.txt').read() cipherbytes = crypto.base64_to_bytes(b64text) keysizes = crypto.brute_keysize(cipherbytes) keyrange = [[byte] for byte in range(0, 255)] # Identify key candidates by trying single byte keys, spaced keysize # bytes apart. key_candidates = [] for keysize in keysizes: chunks = crypto.split_every_n_bytes(cipherbytes, keysize) key = b'' for chunk in chunks: k, text = crypto.brute_xor(chunk, keyrange) key += bytes(k) key_candidates.append(key) # Now run the key candidates and find the winner. key, text = crypto.brute_xor(cipherbytes, key_candidates) print('Decrypted with key: %s' % key.decode()) print('%s' % text)
def c10(): # My own test data message = 'The crow flies from the chicken coop at dawn.' key = b'YeLlOw SuBmArInE' blocksize = len(key) iv = bytes([randrange(0, 256) for i in range(0, blocksize)]) cryptbytes = crypto.encrypt_aes_cbc(crypto.str_to_bytes(message), iv, key) print('Encrypted: %r' % cryptbytes) plainbytes = crypto.decrypt_aes_cbc(cryptbytes, iv, key) print('Decrypted: %r' % plainbytes.decode()) expect(plainbytes.decode(), message) # Decrypt the example from Cryptopals key = b'YELLOW SUBMARINE' blocksize = len(key) iv = bytes([0]*blocksize) cryptbytes = crypto.base64_to_bytes(open('inputs/10.txt').read()) decrypted = crypto.decrypt_aes_cbc(cryptbytes, iv, key) print(decrypted.decode())
def c7(): b64text = open('inputs/7.txt').read() cryptbytes = crypto.base64_to_bytes(b64text) text = crypto.decrypt_aes_ecb(cryptbytes, b'YELLOW SUBMARINE').decode() print('Decrypted:') print('%s' % text)