Search code examples
pythonshellaes

python zip and AES encrypt the output using subprocess


I'm new to python and I would like to do a zip and having the output encrypted in AES via python using subprocess command.

The standard shell command I use is as below:

zip -9 - test.txt | openssl enc -aes-256-cbc -md sha256 -out test.ENC -pass pass:123456

I tried to use in in python like below:

import subprocess
compress = subprocess.Popen(['zip', '-9', 'test.txt'], stdout=subprocess.PIPE,)
subprocess.Popen(['openssl enc -aes-256-cbc -md=sha256 -pass pass:123456 -out', stdin=compress.stdout], stdin=compress.stdout,)

I'm getting an error:

File "test.py", line 3
    subprocess.Popen(['openssl enc -aes-256-cbc -md=sha256 -pass pass:123456 -out test.ENC', stdin=compress.stdout], stdin=compress.stdout,)

Any help please? Thanks.


Solution

  • Problems:

    • In my version of openssl, there is no -aes-256-cbc.
    • '-md=sha256' needs to be '-md', 'sha256'
    • Did you mean to send the output of openssl to a pipe?
    import subprocess
    compressor = subprocess.Popen(
         ['zip', '-9', 'test.txt'], stdout=subprocess.PIPE)
    subprocess.Popen(
            ['openssl', 'enc', '-aes-256-cfb', '-md', 'sha256', '-pass','pass:123456'],
            stdin=compressor.stdout)