Python 3.5 windows x64 and up-to-date libs
I'm having an issue passing an AES key. When its generated from os.urandom and set as a variable it works OR when its set as a variable directly(a = b'\x65'). But, when I try to enter it/get it from an input I get an error about "AES key must be 16,24,32 bytes".
I've tried entering it in the form of
and
both return the same error, at this point I assume it has to do with the python input function. Any ideas for a work around or some modification to the input line? Heres the bit that causes the problem
import os
from Crypto import Random
from Crypto.Cipher import AES
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encrypt(mes, passphrase, key_size=256):
mes = pad(mes)
iv = Random.new().read(AES.block_size)
cipher = AES.new(passphrase, AES.MODE_CBC, iv)
return iv + cipher.encrypt(mes)
def decrypt(ciphertext, passphrase):
iv = ciphertext[:AES.block_size]
cipher = AES.new(passphrase, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return plaintext.rstrip(b"\0")
def encrypt_file(file_name, passphrase):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = encrypt(plaintext, passphrase)
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
def decrypt_file(file_name, passphrase):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = decrypt(ciphertext, passphrase)
with open(file_name[:-4], 'wb') as fo:
fo.write(dec)
passphrase = os.urandom(32)
print("Passphrase:",passphrase)
encrypt_file('foo.txt', passphrase)
input("Press enter to decrypt")
passphrase = input("Enter passprase:")
decrypt_file('foo.txt.enc', passphrase)