Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running OpenSSL System Call

Tags:

python

New to Python and can't get this to work. I need to spawn an openSSL process. Here is what I have:

from subprocess import call

cmd = "openssl aes-128-cbc -d -in ciphertext -base64 -pass pass:test123"
decrypted = call(cmd)
print (decrypted)

This won't even compile. I get TypeError: 'function' object is not subscriptable

Can anyone tell me how I can do this? Thanks.

By the way, when I just type my cmd string into a terminal, it works fine.

EDIT: I changed the line decrypted = call[cmd] to decrypted = call(cmd). When I do that, I get this sequence of errors:

Traceback (most recent call last):
..., line 14, in <module>
    plaintext = call(cmd)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/subprocess.py", line 523, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/subprocess.py", line 817, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/subprocess.py", line 1441, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'openssl aes-128-cbc -d -in test.enc -base64 -pass pass:hello'
like image 637
AndroidDev Avatar asked Mar 04 '26 03:03

AndroidDev


1 Answers

you use paranthesis instead of square brackets

i.e.:

decrypted = call(cmd)

More generally, you use parenthesis to wrap the arguments in a function call in python (as well as in most other main-stream languages).

Also, by default call treats the first argument as the executable to run, without any argument. You either need to pass shell=True as well, or split the command up into an array and pass that.

decrypted = call(cmd, shell=True) #execute command with the shell
# or
decrypted = call(['openssl', 'aes-128-cbc', '-d', '-in', 'ciphertext', '-base64', '-pass', 'pass:test123'])

In general the latter is preferred because it takes care of escaping for you, and is more portable between different shells.

like image 147
Thayne Avatar answered Mar 05 '26 16:03

Thayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!