React Native CryptoJS为AES-256-CBC解密提供空值 [英] React Native CryptoJS giving empty value for AES-256-CBC decryption
问题描述
我正在使用此 https://github.com/imchintan/react-用于React Native CryptoJS的native-crypto-js 包,我使用了此在线工具 https://www.javainuse.com/aesgenerator 生成以下示例数据:
I'm using this https://github.com/imchintan/react-native-crypto-js package for React Native CryptoJS and I used this online tool https://www.javainuse.com/aesgenerator to generate this example data:
const encryptedData = {
cipher: "OuCmv1nXCzfy+529oeJU8g==",
iv: "1234123412341234",
key: "56785678567856785678567856785678"
}
在对所选模式进行加密时, CBC
,密钥大小为 256
位,输出格式为 base64
我正在解密它:
While encrypting the Mode selected is CBC
, the Key Size is 256
bits, the output format is base64
in react native this is how I'm decrypting it:
let bytes = CryptoJS.AES.decrypt(encryptedData.cipher, encryptedData.key);
let originalText = bytes.toString(CryptoJS.enc.Utf8);
console.log("Text is: " + originalText);
但是我得到的只是文本是:
.
推荐答案
当您向CryptoJS密码提供实际上是字符串的密钥"时,会将其视为密码短语,并使用与OpenSSL兼容的基于密码的密钥派生<在中指定的代码> enc .您需要将密钥和iv转换为 WordArray
,如
When you provide a CryptoJS Cipher a 'key' that is actually a string, it treats it as a passphrase and uses password-based key derivation compatible with OpenSSL enc
as specified in the documentation. You need to convert the key and iv to WordArray
s much as shown just above that, plus I can't find how to make the 'configurable' parsing work so I also parse explicitly as shown just below.
const CryptoJS = require('crypto-js');
var key = CryptoJS.enc.Latin1.parse("56785678567856785678567856785678")
var iv = CryptoJS.enc.Latin1.parse("1234123412341234")
var ctx = CryptoJS.enc.Base64.parse("OuCmv1nXCzfy+529oeJU8g==")
var enc = CryptoJS.lib.CipherParams.create({ciphertext:ctx})
console.log( CryptoJS.AES.decrypt (enc,key,{iv:iv}) .toString(CryptoJS.enc.Utf8) )
->
Talha
从技术上讲,像 parse
这样的 decrypt
的结果是一个 WordArray
,因此称它为 bytes
有点误导和混乱.
Technically the result of decrypt
, like parse
, is a WordArray
, so calling it bytes
is somewhat misleading and confusing.
这篇关于React Native CryptoJS为AES-256-CBC解密提供空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!