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

时间:2016-09-22 11:09:27

标签: linux encryption openssl cryptography libssl

我已经下载并编译了<img src="" alt="" style="width: 100%;"> <p>Test text loren is the ipsum</p>

我可以使用openssl-1.1.0的同一个exe加密和解密(就像here

openssl

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

当我尝试使用我的ubuntu上安装的libcrypto.so.1.1, libssl.so.1.1解密时,它使用: openssl

我收到错误:

/lib/x86_64-linux-gnu/libssl.so.1.0.0, /lib/x86_64-linux-gnu/libcrypto.so.1.0.0

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

4 个答案:

答案 0 :(得分:85)

默认摘要在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 Use SHA256 not MD5 as default digest.

中更改的消息摘要

如果你想保持简单的密码,而不是开始搞乱键控武术(-K,-iv),只需用 -md

强制使用相同的摘要

答案 1 :(得分:3)

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

-p的2个不同版本使用openssl选项时,IV和密钥不同:

$ 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个版本的密钥和IV的不同推导。

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

答案 2 :(得分:2)

openssl会抛出各种错误字符串,具体取决于各自的版本和方案。以下是在与openssl相关的情况下使用的清单:

  1. 理想情况下,openssl只能使用相同的密钥(+盐)和enc算法进行加密/解密。
  2. 确保openssl版本(用于加密/解密)兼容。例如。 openssl中使用的哈希在版本1.1.0中从MD5更改为SHA256。这将产生与相同密码不同的密钥。 固定: 在1.1.0中添加“ -md md5”以解密较低版本的数据,并且 在较低版本中添加“ -md sha256”以从1.1.0解密数据

  3. 确保您的计算机中安装了单个openssl版本。如果同时安装了多个版本(在我的机器上,这些版本已安装:-'LibreSSL 2.6.5'和'openssl 1.1.1d'),请确保PATH变量中仅出现所需的版本。

答案 3 :(得分:1)

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

您应该使用-md sha256或所有版本支持的其他更安全的消息摘要。 -md md5只应用于解密旧文件,理想情况下应使用sha256重新加密。 the OpenSSL FAQ中也提到了这一点。

要查看您正在使用的不同版本支持哪些邮件摘要,请运行openssl help

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
…
相关问题