具有十六进制密钥的CryptoJS无法正确解密 [英] CryptoJS with hex key not decrypting properly

查看:121
本文介绍了具有十六进制密钥的CryptoJS无法正确解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加密的数据集,正在尝试使用CryptoJS解密.我尝试了各种组合,但是由于某种原因,结果不是我所期望的.我在下面提供了密钥以及我想解密的文本.我在msg1上期望的长度是32个字符,但我一直得到48个字符.截至其为止,它会额外填充16个字符.

I have a dataset that was encrypted that I'm trying to decrypt with CryptoJS. I've tried various combination of things but for some reason the result is not what I'm expecting. I have provided the key below and also the text I want to decrypt. What I'm expecting at msg1 is 32 characters long but I keep getting 48. Its as of its padding it with an extra 16 characters.

预先感谢您的帮助.

key = 'd13484fc2f28fd0426ffd201bbd2fe6ac213542d28a7ca421f17adc0cf234381';
text = '8bf3955488af91feb7bd87220910cee0';


decrypt(text: string): void{

       let msg1 = CryptoJS.AES.decrypt(text, CryptoJS.enc.Hex.parse(key), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding});
       msg1 = CryptoJS.enc.Hex.stringify(msg1 );

}

推荐答案

解决这个问题很简单,但是阅读文档和代码后,我不清楚原因为何.

Solving it is pretty simple, but reading the docs and the code, I'm not quite clear why.

这显然是错误的:

let msg1 = CryptoJS.AES.decrypt(text, CryptoJS.enc.Hex.parse(key), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding});

根据您的描述,您期望由十六进制数字"8bf3955488af91feb7bd87220910cee0"表示的字节序列为正文.但这不是你的过世.您正在传递字符.因此,当解密它时,第一个字节是 8 (0x38)的ASCII值,而不是0x8b.鉴于此,您应该像这样解析十六进制:

Given your description, you are expecting the byte sequence represented by the hex digits "8bf3955488af91feb7bd87220910cee0" to be the body. But that's not what your passing. You're passing the characters. So when it decrypts it, the first byte is the ASCII value of 8 (0x38), not 0x8b. Given that, you should be parsing the hex like this:

let msg1 = CryptoJS.AES.decrypt(CryptoJS.enc.Hex.parse(text), ...

但是,由于某种原因,我无法理解,所以不起作用. decrypt 需要Base64(或至少它将接受Base64).我找不到任何说明这一点的文档,并且代码以我无法完全理解的方式神奇地创建了 decrypt 函数,这就是为什么我真的讨厌在JavaScript中进行加密工作.

But, for reasons I'm having trouble understanding, that doesn't work. decrypt expects Base64 (or at least it will accept Base64). I can't find any documentation that says this, and the code creates the decrypt function magically in a way that I don't fully understand, and this is why I really hate doing crypto work in JavaScript.

这已经不在我的系统中了,所以让我们开始答案吧

That's out of my system now, so let's get to the answer:

cipher = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(text))

let msg1 = CryptoJS.AES.decrypt(cipher, CryptoJS.enc.Hex.parse(key), { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding});

那应该会给您期望的结果.

And that should give you the result you're expecting.

这篇关于具有十六进制密钥的CryptoJS无法正确解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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