2

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?

  • 1
    Possible duplicate of [How to AES encrypt/decrypt files using Python/PyCrypto in an OpenSSL-compatible way?](http://stackoverflow.com/questions/16761458/how-to-aes-encrypt-decrypt-files-using-python-pycrypto-in-an-openssl-compatible) – Gregor Nov 01 '15 at 05:19

1 Answers1

0

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
Yasir Malik
  • 441
  • 2
  • 9
  • thanks, but it doesn't help. i still getting `bad magic number`. – nekolyanich Mar 20 '13 at 06:56
  • you have to provide the key used and iv in command line too. Kinldy check this post. http://stackoverflow.com/questions/8343894/aes-encrypt-with-openssl-command-line-tool-and-decrypt-in-java – Yasir Malik Mar 20 '13 at 07:02