Steps:
thanks to Piotr Dabkowski and Karin
# Crosscompute
secret_key = 'secret key'
target_text_path= 'm.txt'
encrypt_or_decrypt_select = '''
encrypt
encrypt
decrypt'''
target_folder = '.'
encrypt_or_decrypt = [x.strip() for x in
encrypt_or_decrypt_select.split('\n') if x.strip()][0]
l = len(secret_key)
filler = '@'
accepted_lengths = sorted([16, 24, 32])
for byte_length in accepted_lengths:
if l < byte_length:
secret_key += (filler * (byte_length - l))
break
else:
secret_key = secret_key[:32]
from Crypto.Cipher import AES
def encrypt(k, m):
aes = AES.new(k, AES.MODE_CFB, 'This is an IV456')
return aes.encrypt(m)
def decrypt(k, ciphertext):
aes = AES.new(k, AES.MODE_CFB, 'This is an IV456')
return aes.decrypt(ciphertext)
from ast import literal_eval
with open(target_text_path, 'r', encoding='utf-8') as f:
if encrypt_or_decrypt == 'decrypt':
source = f.read()
target_text = literal_eval("b'{}'".format(source))
print('plain text = %s' % decrypt(secret_key, target_text))
else:
target_text = f.read()
print('ciphertext = %s' % encrypt(secret_key, target_text))