OpenSSL和CryptoJS SHA256加密转换 [英] OpenSSL and CryptoJS SHA256 encryption conversion

查看:536
本文介绍了OpenSSL和CryptoJS SHA256加密转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是OpenSSL的较新版本与CryptoJS的默认设置不兼容。

My problem is that the newer versions of OpenSSL aren't compatible with default settings of CryptoJS.


openssl使用的默认哈希基于密码的密钥派生
的enc在1.1.0中更改为SHA256,而在较低版本中更改为MD5。
https://unix.stackexchange.com/questions/344150/why-can-one-box-decrypt-a-file-with-openssl-but-another-one-cant/344586#344586

默认情况下, CryptoJS 使用MD5作为其密钥派生工具。 OpenSSL使用的是MD5,但现在在OpenSSL版本> = 1.1.0中,它使用的是SHA256。

By default, CryptoJS uses MD5 for its key derivation. OpenSSL used MD5, but now in OpenSSL versions >=1.1.0 it's using SHA256.

因此,如果我通过 -md md5 到OpenSSL,CryptoJS兼容:

So if I pass -md md5 to OpenSSL, CryptoJS is compatible:

echo "Hello World" | openssl enc -aes-256-cbc -md md5 -pass pass:"Secret Passphrase" -e -base64

输出: U2FsdGVkX19aufvaqQQ89scaApBos6oFCyqPj7IKUFk =

CryptoJS:

CryptoJS.AES.decrypt('U2FsdGVkX19aufvaqQQ89scaApBos6oFCyqPj7IKUFk=', 'Secret Passphrase').toString(CryptoJS.enc.Utf8);

输出: Hello World

但是现在,如果我想使用SHA256而不是MD5(删除 -md md5 ):

But now if I want to use SHA256 instead of MD5 (removing the -md md5):

echo "Hello World" | openssl enc -aes-256-cbc -pass pass:"Secret Passphrase" -e -base64

输出: U2FsdGVkX1 / 5LLkFkTpawh1im4a / fCco5hS42cjn / fg =

CryptoJS:

CryptoJS.AES.decrypt('U2FsdGVkX1/5LLkFkTpawh1im4a/fCco5hS42cjn/fg=', 'Secret Passphrase').toString(CryptoJS.enc.Utf8);

输出:null

我如何告诉CryptoJS对其密钥派生使用SHA256而不是MD5?

How do I tell CryptoJS to use SHA256 instead of MD5 for its key derivation?

推荐答案

似乎CryptoJS不能按原样提供这种灵活性。这是因为MD5的使用被硬编码到用于从密码短语中获取密钥的功能中。您可以在OpenSSLKdf中此处看到它

It seems that CryptoJS "as-is" does not provide this flexibility. This is because the use of MD5 is hard coded into the function used to derive the key from the passphrase. You can see it happening here in OpenSSLKdf:

var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);

KDF代表密钥派生功能,创建时不提供摘要。 EvpKDF.create()本身确实有可能用于不同类型的摘要,如您在其配置选项

KDF stands for Key Derivation Function, and no digest is provided when it is created. That EvpKDF.create() in itself does have the possibility for different types of digests, as you can see in its configuration options:

        cfg: Base.extend({
            keySize: 128/32,
            hasher: MD5,
            iterations: 1
        }),

它还显示MD5是默认值。

It also shows that MD5 is the default.

所有这意味着如果将第一行修改为以下内容,解密将起作用:

All of this implies that the decryption will work if you modify that first line to the following:

var key = EvpKDF.create({ keySize: keySize + ivSize, hasher: C_algo.SHA256 }).compute(password, salt);

实际上,在编辑(作为测试)该文件后, cipher-core .js 在本地安装的模块中,最后一行代码确实会产生所需的 Hello World

and indeed, after editing (as a test) that file cipher-core.js in your locally installed module, your last line of code does result in the desired Hello World.

我看到无需修改CryptoJS模块本身即可重现此代码的唯一方法是将模块中的整个代码块复制到自己的代码中。

The only way I see to reproduce this without modifying the CryptoJS module itself is by duplicating a whole chunk of code from the module into your own code.

这篇关于OpenSSL和CryptoJS SHA256加密转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆