CTF-spied-conversation/encryption.py

27 lines
No EOL
715 B
Python

import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
key = os.urandom(16)
# iv = os.urandom(16)
# iv_2 = os.urandom(16)
iv_1 = b'\x00' * 16
iv_2 = b'\x00' * 16
cipher = Cipher(algorithms.AES(key), modes.CBC(iv_1))
print(f"block size AES: {algorithms.AES.block_size}")
encryptor = cipher.encryptor()
ciphertext = encryptor.update(b"a secret message") + encryptor.finalize()
print(f"ciphertext={ciphertext!r}")
print(f"len(ciphertext)={len(ciphertext)}")
cipher_2 = Cipher(algorithms.AES(key), modes.CBC(iv_2))
decryptor = cipher_2.decryptor()
res = decryptor.update(ciphertext) + decryptor.finalize()
print(f"res={res!r}")