我有这个javascript。当我试图解密它没有给出任何输出。 加密工作正常但不解密 我做错了什么。
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script language = "javascript">
var JsonFormatter = {
stringify: function (cipherParams) {
// create json object with ciphertext
var jsonObj = {
ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
};
// optionally add iv and salt
if (cipherParams.iv) {
jsonObj.iv = cipherParams.iv.toString();
}
if (cipherParams.salt) {
jsonObj.s = cipherParams.salt.toString();
}
// stringify json object
return JSON.stringify(jsonObj);
},
parse: function (jsonStr) {
// parse json string
var jsonObj = JSON.parse(jsonStr);
// extract ciphertext from json object, and create cipher params object
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
});
// optionally extract iv and salt
if (jsonObj.iv) {
cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv)
}
if (jsonObj.s) {
cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s)
}
return cipherParams;
}
};
var AES = CryptoJS.AES;
var key = "B374A26A71490437AA024E4FADD5B497FDFF1A8EA6FF12F6FB65AF2720B59CCF";
var iv = CryptoJS.enc.Hex.parse('7E892875A52C59A3B588306B13C31FBD');
var aeskey = CryptoJS.enc.Hex.parse(key);
var secret = "50585";
alert("attempt 1");
var e1 = AES.encrypt(secret, aeskey, { iv: iv , format: JsonFormatter });
var encJSON = JSON.parse(e1);
var encresult = encJSON['ct'];
var encres = encodeURIComponent(encresult);
alert("encrypted = " + encresult);
alert("e1.iv = " + e1.iv);
alert("encoded = " + encres);
alert("attempt 2");
var decoderesult = decodeURIComponent(encres);
var encObj = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(decoderesult)
});
var decrypt = AES.decrypt(encObj, aeskey, { iv: iv , format: JsonFormatter });
var decrypted = decrypted.toString(CryptoJS.enc.Utf8);
alert("decoded = " + decoderesult);
alert("decrypted = " + decrypted);
alert("decrypt.iv = " + decrypt.iv);
</script>
加密警报即将到来但不是解密。失败的原因和地点
答案 0 :(得分:-1)
我认为var decrypted = decrypted.toString(CryptoJS.enc.Utf8);
行中只有一个拼写错误var decrypted = decrypt.toString(CryptoJS.enc.Utf8)
。
在此之后,您将获得decoderesult
和decrypted
。但是decrypt.iv
仍然返回undefined。不确定这里有什么问题。
有关演示,请参见下文和jsFiddle。
var log = function(text) {
output.innerHTML += '<p>' + text + '</p>\n';
}
var JsonFormatter = {
stringify: function (cipherParams) {
// create json object with ciphertext
var jsonObj = {
ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
};
// optionally add iv and salt
if (cipherParams.iv) {
jsonObj.iv = cipherParams.iv.toString();
}
if (cipherParams.salt) {
jsonObj.s = cipherParams.salt.toString();
}
// stringify json object
return JSON.stringify(jsonObj);
},
parse: function (jsonStr) {
// parse json string
var jsonObj = JSON.parse(jsonStr);
// extract ciphertext from json object, and create cipher params object
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
});
// optionally extract iv and salt
if (jsonObj.iv) {
cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv)
}
if (jsonObj.s) {
cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s)
}
return cipherParams;
}
};
var AES = CryptoJS.AES;
var key = "B374A26A71490437AA024E4FADD5B497FDFF1A8EA6FF12F6FB65AF2720B59CCF";
var iv = CryptoJS.enc.Hex.parse('7E892875A52C59A3B588306B13C31FBD');
var aeskey = CryptoJS.enc.Hex.parse(key);
var secret = "50585";
//alert("attempt 1");
var e1 = AES.encrypt(secret, aeskey, { iv: iv , format: JsonFormatter });
var encJSON = JSON.parse(e1);
var encresult = encJSON['ct'];
var encres = encodeURIComponent(encresult);
console.log('encres', encresult);
console.log('e1.iv=', e1.iv);
console.log('encoded=', encres);
log('encres= ' + encresult);
log('e1.iv=' + e1.iv);
log('encoded=' + encres);
//alert("encrypted = " + encresult);
//alert("e1.iv = " + e1.iv);
//alert("encoded = " + encres);
//alert("attempt 2");
var decoderesult = decodeURIComponent(encres);
var encObj = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(decoderesult)
});
var decrypt = AES.decrypt(encObj, aeskey, { iv: iv , format: JsonFormatter });
console.log(decrypt);
var decrypted = decrypt.toString(CryptoJS.enc.Utf8); //decrypted --> decrypt
console.log("decoded = " + decoderesult);
console.log("decrypted = " + decrypted);
console.log("decrypt.iv = " + decrypt.iv);
log("decoded = " + decoderesult);
log("decrypted = " + decrypted);
log("decrypt.iv = " + decrypt.iv);
&#13;
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<div id="output"></div>
&#13;