【问题标题】:Encryption/decryption doesn't work well between two different openssl versions加密/解密在两个不同的 openssl 版本之间不能很好地工作
【发布时间】:2017-01-30 22:24:47
【问题描述】:

我已经下载并编译了openssl-1.1.0

我可以使用openssl 的相同exe 进行加密和解密(here 也是如此)

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/libcrypto.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:

什么可能导致这种情况? 谢谢

【问题讨论】:

标签: linux encryption openssl cryptography libssl


【解决方案1】:

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 强制使用相同的摘要

【讨论】:

  • 一天中最好的时间都在想我们的代码坏了。偶然发现你的答案。拯救了这一天!
  • C++程序中如何强制md5?
  • 是的,指定摘要 -md md5 有效,但是更好的解决方案是使用 -md sha256 重新加密(解密也一样),这比 md5 更安全,因此新版本的默认摘要发生了变化的openssl
  • 不要使用md5。它不安全且损坏。
  • +1,是的,较新的默认值显然更好(md5 也存在冲突问题),但在我的情况下,我需要使用它来解密使用旧标准加密的旧文件
【解决方案2】:

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

-p 选项与openssl 的2 个不同版本一起使用时,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 选项并在您的 openssl 命令中使用选项 -K-iv 作为密钥。

【讨论】:

    【解决方案3】:

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

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

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


    要检查您使用的不同版本支持哪些消息摘要,请运行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
    …
    

    【讨论】:

      【解决方案4】:

      根据各自的版本和场景,openssl 会抛出各种错误字符串。以下是我在遇到 openssl 相关问题时使用的清单:

      1. 理想情况下,openssl 只能使用相同的密钥(+ salt)和加密算法进行加密/解密。
      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 变量中。

      【讨论】:

        猜你喜欢
        • 2016-09-05
        • 2011-10-24
        • 2017-10-11
        • 2021-11-07
        • 1970-01-01
        • 2018-01-17
        • 1970-01-01
        • 2014-07-19
        • 1970-01-01
        相关资源
        最近更新 更多