Search code examples
pythonopensslaespycrypto

openssl not decrypted files encrypted by pycrypto


i encrypt file in python:

from Crypto.Cipher import AES
from Crypto import Random
key = Random.new().read(16)
iv = Random.new().read(AES.block_size)
encryptor = AES.new(key, AES.MODE_CBC, iv)
with open(in_file, 'rb') as fin, open(out_file, 'wb') as fout:
    fout.write(iv)
    while True:
        chunk = fin.read(16*1024)
        if len(chunk) == 0:
             break
        elif len(chunk) % 16 != 0:
             chunk += b' ' * (16 - len(chunk) % 16)
        fout.write(encryptor.encrypt(chunk)
print base64.b32encode(key)

but when after what i try decrypt it with openssl: openssl aes-256-cbc -d -in enc -out new.zip it returns bag magic number what i doing wrong?


Solution

  • Program is using 128 bit AES key for encryption, as seen from the line:

    key = Random.new().read(16)
    

    So, instead of using

    openssl aes-256-cbc -d -in enc -out new.zip
    

    use this

    openssl aes-128-cbc -d -in enc -out new.zip