数字信封例程:EVP_DecryptFinal_ex:decryp错误

2024-06-28 15:46:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个Python程序,运行OpenSSL命令来加密和解密文件。在

在命令行中我做了:

vagrant@vagrant:~$ openssl enc -aes256 -base64 -k $(base64 alice_shared_secret.bin) -e -in plain.txt -out cipher.txt
vagrant@vagrant:~$ openssl enc -aes256 -base64 -k $(base64 bob_shared_secret.bin) -d -in cipher.txt -out plain_again.txt

而且很管用。 但是,如果我用Python程序加密文本,就无法解密。在

^{pr2}$

我的程序和命令行的密码有什么区别?在

代码:

#!/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python
openssl         = '/usr/bin/openssl'
from subprocess import Popen, PIPE, STDOUT

def encrypt():
  arguments = [
    openssl,
    'enc',
    '-aes256',
    '-base64',
    '-k',
    '$(base64 alice_shared_secret.bin)',
    '-e', 
    '-in',
    'plain.txt',
    '-out',
    'cipher.txt',
  ]

  execute = Popen(arguments, stdout=PIPE)
  out, err = execute.communicate()

encrypt()

有什么帮助吗?在


Tags: 命令行in程序txtsecretbinoutaes256
1条回答
网友
1楼 · 发布于 2024-06-28 15:46:30

如果您不能使用来自Popen参数(您的“$(…)”表达式)的shell字符串插值。尝试直接编码base64数据,它会很好地工作:

import base64

with open('secret.bin') as x: secret = x.read()

def encrypt():
  arguments = [
    openssl,
    'enc',
    '-aes256',
    '-base64',
    '-k',
    base64.b64encode(secret),
    '-e', 
    '-in',
    'plain.txt',
    '-out',
    'cipher.txt',
  ]

相关问题 更多 >