from Crypto.Cipher import AES
iv = "1111222233334444".encode('UTF-8')
key = "aaaabbbbccccdddd".encode('UTF-8')

cipher = AES.new(key, AES.MODE_CBC, iv)
# Szyfrowanie w trybie CBC
with open("./samolot.bmp", "rb") as f:
    byteblock = f.read()

pad = len(byteblock)%16 * -1
byteblock_trimmed = byteblock[64:pad]
ciphertext = cipher.encrypt(byteblock_trimmed)
ciphertext = byteblock[0:64] + ciphertext + byteblock[pad:]

with open("./e_samolot_cbc.bmp", "wb") as f:
    f.write(ciphertext)

# Odszyfrowywanie  
cipher = AES.new(key, AES.MODE_CBC, iv)

with open("./e_samolot_cbc.bmp", "rb") as f:
    byteblock = f.read()

pad = len(byteblock)%16 * -1
byteblock_trimmed = byteblock[64:pad]
plaintext = cipher.decrypt(byteblock_trimmed)
plaintext = byteblock[0:64] + plaintext + byteblock[pad:]

with open("./d_samolot_cbc.bmp", "wb") as f:
    byteblock = f.write(plaintext)
print("Gotowe")
