提问者:小点点

加密/解密在两个不同的openssl版本之间不能很好地工作


我已经下载并编译了< code>openssl-1.1.0。

我可以使用与< code>openssl相同的exe进行加密和解密

me@ubuntu:~/openssl-1.1.0$ LD_LIBRARY_PATH=. ./apps/openssl aes-256-cbc -a -salt -in file.txt -out file.txt.enc
enter aes-256-cbc encryption password: 123
Verifying - enter aes-256-cbc encryption password:
me@ubuntu:~/openssl-1.1.0$ LD_LIBRARY_PATH=. apps/openssl aes-256-cbc -a -d -in file.txt.enc -out file.txt.dec
enter aes-256-cbc decryption password: 123

这个openssl使用:libcrypto.so.1.1,libssl.so.1.1

当我尝试使用ubuntu上安装的openssl进行解密时,它使用:/lib/x86_64-linux-gnu/libssl.so.1.0.0、/lib/x86_64-linux-gnu-libscrypto.so.1.0.0

我收到一个错误:

me@ubuntu:~/openssl-1.1.0$ openssl aes-256-cbc -a -d -in file.txt.enc -out file.txt.dec2
enter aes-256-cbc decryption password: 123
bad decrypt
140456117421728:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:

这可能是什么原因造成的?谢谢


共3个答案

匿名用户

在Openssl 1.1中,默认摘要从MD5更改为SHA256

尝试使用-md md5

cgs@ubuntu:~$ echo "it-works!" > file.txt
cgs@ubuntu:~$ LD_LIBRARY_PATH=~/openssl-1.1.0/ openssl-1.1.0/apps/openssl aes-256-cbc -a -salt -in ~/file.txt -out ~/file.txt.enc -md md5
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
cgs@ubuntu:~$ LD_LIBRARY_PATH=~/openssl-1.0.1f/ openssl-1.0.1f/apps/openssl aes-256-cbc -a -in ~/file.txt.enc -d
enter aes-256-cbc decryption password:
it-works!

丑陋的细节:

aes(或其他加密)不会直接使用输入的密码,但该命令会从中隐式导出一个密钥。密钥派生使用openssl 1.1中更改的消息摘要。使用SHA256而不是MD5作为默认摘要。

如果你想保持简单的密码,而不是开始搞乱键控军事(-K,-iv),只需使用-md强制相同的摘要

匿名用户

我用 1.1.0a 版(从 openssl.org 下载)和 1.0.2g-fips(从我的 ubuntu 16.04 下载)测试 AES 加密和解密

当在 2 个不同版本的 openssl(开盘)上使用 -p 选项时,IV 和 key 是不同的:

$ LD_LIBRARY_PATH=~/openssl-1.1.0a/ ~/openssl-1.1.0a/apps/openssl aes-256-cbc -a -p -salt -in file -out file.enc
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
salt=6A80B2A3B4CFE048
key=637E17094DF7892A7AFC14957EAA13991DFFD3273A2459EDA613F3AD8A406C38
iv =6AC7CE5C9AADC6C46C633BF5124DAFBF

$ openssl aes-256-cbc -a -d -p -in file.enc -out file.dec
enter aes-256-cbc decryption password:
salt=6A80B2A3B4CFE048
key=6220AF2E25CB0B5D9994A0A1B05503D82AC5B0B4C9015E241CACBF8BF62DAC77
iv =2DC04EF29AA57478EBE606DF87277EA6
bad decrypt
140557073118872:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:592:

我怀疑基于2个版本的salt,key和IV有不同的派生。

如果要消除此解密错误,可以删除-salt选项,并在openssl命令中使用-K作为密钥和-iv

匿名用户

OpenSSL 1.1和LibreSSL之间也可能出现此问题。在这种情况下,以及在其他更安全的消息摘要可用的情况下,应避免使用-md-md5加密新文件,因为md5算法存在大量漏洞。

您应该改用-mdsha256或其他所有版本都支持的更安全的消息摘要-md-md5只能用于解密旧文件,理想情况下应该使用sha256对其进行重新加密。这在OpenSSL常见问题解答中也有提及:

报文摘译用于从人工输入的密码短语创建加密/解密密钥。在OpenSSL 1.1.0中,我们从MD5更改为SHA-256。我们这样做是为了摆脱现在不安全和损坏的MD5算法的整体更改的一部分。如果您有旧文件,请使用“-md md5”标志来解密它们。

要检查您正在播放的不同版本支持哪些消息摘要,请运行 openssl 帮助

LibreSSL 2.2.7(包含在macOS 10.13 High Sierra中):

$ openssl help
…
Message Digest commands (see the `dgst' command for more details)
gost-mac          md4               md5               md_gost94
ripemd160         sha               sha1              sha224
sha256            sha384            sha512            streebog256
streebog512       whirlpool
…

OpenSSL 1.1f:

$ openssl help
…
Message Digest commands (see the `dgst' command for more details)
blake2b512        blake2s256        gost              md4
md5               rmd160            sha1              sha224
sha256            sha384            sha512
…