如何使用AES解密CryptoJS? [英] How to decrypt with CryptoJS using AES?
问题描述
m加密如下:
var ENC_KEY =bXlrZXk =; //mykey
var encrypted = CryptoJS.AES.encrypt(hello,CryptoJS.enc.Base64.parse(ENC_KEY),
{
mode:CryptoJS。 mode.ECB,
padding:CryptoJS.pad.Pkcs7
});
console.log('encrypted:'+ encrypted);
它可以按预期工作,并输出我预期的加密值,但是当我使用下面的解密方法最后输出一个空对象:
var decryptpted = CryptoJS.AES.decrypt(encrypted,CryptoJS.enc.Base64。解析(ENC_KEY),
{
模式:CryptoJS.mode.ECB,
padding:CryptoJS.pad.Pkcs7
});
console.log('decryptpted:'+ decryptpted);
我也试过使用:
console.log('encrypted解密为:'+ decryptpted.toString(CryptoJS.enc.Utf8);
但没有喜悦...
我在一个小提琴有一点让它工作):
// decrypt给出一个十六进制
函数hex2a(hexx){
var hex = hexx.toString(); //强制转换
var str ='';
for(var i = 0; i< hex.length; i + = 2)
str + = String.fromCharCode(parseInt(hex.substr(i,2),16));
return str;
}
var key = CryptoJS。 encBase64.parse(Secret Passphrase);
alert(key);
var encrypted = CryptoJS.AES.encrypt(hello,key,
{
mode:CryptoJS.mode.ECB
});
alert('encrypted:'+ encrypted);
var decryptpted = CryptoJS.AES.decrypt(encrypted,key,
{
mo de:CryptoJS.mode.ECB
});
alert('decryptpted:'+ hex2a(decryptpted));
,发现解密返回一个十六进制,这可能不是一个字符串...可以这个引起你的问题?因此,使用快速的hex2ascii函数,'hello'返回:)
我还删除了指定的填充,因为Pkcs7被认为是文档中的默认值,我找不到src js我需要下载。
As the question suggests, I can't seem to get a decrypted value correctly using the required options (AES, ECB Mode & PKCS7).
I'm encrypting as below:
var ENC_KEY = "bXlrZXk="; //"mykey"
var encrypted = CryptoJS.AES.encrypt("hello", CryptoJS.enc.Base64.parse(ENC_KEY),
{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
console.log('encrypted: ' + encrypted);
which works as expected and outputs the encrypted value I expect however when I decrypt this using the below, I end up with an empty object being output:
var decrypted = CryptoJS.AES.decrypt(encrypted, CryptoJS.enc.Base64.parse(ENC_KEY),
{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
console.log('decrypted: ' + decrypted);
I have also tried using:
console.log('encrypted is decrypted to: ' + decrypted.toString(CryptoJS.enc.Utf8);
but no joy...
I tried this in a fiddle(modded a bit to get it to work):
//decrypt gives a hex
function hex2a(hexx) {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
var key = CryptoJS.enc.Base64.parse("Secret Passphrase");
alert(key);
var encrypted = CryptoJS.AES.encrypt("hello", key,
{
mode: CryptoJS.mode.ECB
});
alert('encrypted: ' + encrypted);
var decrypted = CryptoJS.AES.decrypt(encrypted, key,
{
mode: CryptoJS.mode.ECB
});
alert('decrypted: ' + hex2a(decrypted));
and found that decrypt returned a hex, which may not have been a string... could this be causing your problems? So, with a quick hex2ascii function, 'hello' is return back :)
I also removed the padding specified, since Pkcs7 is said to be the default in the docs and I couldn't find src js i needed to download for it.
这篇关于如何使用AES解密CryptoJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!