代码之家  ›  专栏  ›  技术社区  ›  AndroidDev

运行OpenSSL系统调用

  •  3
  • AndroidDev  · 技术社区  · 12 年前

    Python新手,无法使用。我需要创建一个openSSL进程。以下是我所拥有的:

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

    这甚至无法编译。我明白了 TypeError: 'function' object is not subscriptable

    有人能告诉我怎么做吗?谢谢

    顺便说一句,当我在终端中键入cmd字符串时,它工作正常。

    编辑:我换了行 decrypted = call[cmd] decrypted = call(cmd) 。当我这样做时,我会得到以下错误序列:

    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'
    
    1 回复  |  直到 12 年前
        1
  •  4
  •   Thayne    12 年前

    你用括号代替方括号

    即。:

    decrypted = call(cmd)
    

    更一般地,在python(以及大多数其他主流语言)的函数调用中使用括号来包装参数。

    此外,默认情况下,调用将第一个参数视为要运行的可执行文件,而不包含任何参数。你要么需要通过 shell=True 或者将命令拆分成一个数组并传递。

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

    通常情况下,后者是首选的,因为它可以为您提供逃生服务,并且在不同的外壳之间更方便携带。