· 6 years ago · Oct 18, 2019, 12:18 PM
1from Crypto.Cipher import AES
2import os
3from binascii import hexlify
4
5BLOCK_SIZE = 16
6def pad(raw):
7 if len(raw) % 16>0:
8 return raw + bytes(16-(len(raw)%16))
9 return raw
10
11def aes_enc(key, msg):
12 cipher = AES.new(key, AES.MODE_ECB)
13 return cipher.encrypt(msg)
14
15secret_key = os.urandom(32) # 32 bytes secret (AES-256)
16print('Secret KEY='+str(hexlify(secret_key).upper()))
17
18f = open('BLK.BMP','rb')
19plain_text = f.read()
20f.close()
21print('plain_text bytes='+str(len(plain_text)))
22plain_text_padded = pad(plain_text) # Padding plain text to 16 block size
23print('plain_text_padded bytes='+str(len(plain_text_padded)))
24
25cypher_text = aes_enc(secret_key, plain_text_padded)
26print(hexlify(cypher_text))