aes_in_ctr_mode.py (943B)
1 from Crypto.Cipher import AES 2 from Crypto.Util import Counter 3 ciphertext = "L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==".decode("base64") 4 key = "YELLOW SUBMARINE" 5 nonce = ''.join(['x00' for i in range(AES.block_size / 2)]) 6 7 def encrypt(ciphertext, key, nonce): 8 aes = AES.new(key, AES.MODE_ECB) 9 block_count = ['x00' for i in range(AES.block_size / 2)] 10 plaintext = "" 11 12 for block in [ciphertext[i:i + AES.block_size] for i in range(0, len(ciphertext), AES.block_size)]: 13 plaintext += ''.join([chr(ord(a) ^ ord(b)) for a, b in zip(block, aes.encrypt(nonce + ''.join(block_count)))]) 14 block_count[0] = chr((ord(block_count[0]) + 1) % 256) #you get the point 15 16 return plaintext 17 18 print encrypt(encrypt(encrypt(ciphertext, key, nonce), key, nonce), key, nonce) 19 print AES.new(key, AES.MODE_CTR, counter=Counter.new(64,initial_value=0,little_endian=True,prefix=nonce)).encrypt(ciphertext)