我正在尝试制作一个运行OpenSSL命令的Python程序,用于对文件进行加密和解密。
在命令行中,我做了:
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程序加密文本,我就无法解密。
vagrant@vagrant:~$ openssl enc -aes256 -base64 -k $(base64 bob_shared_secret.bin) -d -in cipher.txt -out plain_again.txt
bad decrypt
140321401345688:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:529:我的程序和命令行的密码有什么区别?
代码:
#!/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()有什么帮助吗?
发布于 2019-05-26 20:46:33
AFAIK您不能使用来自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',
]https://stackoverflow.com/questions/56314626
复制相似问题