我有這個javascript。當我試圖解密時,它沒有給出任何輸出。 加密工作正常但不解密 我在做什麼錯誤。解密與cryptojs失敗
<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>
加密警報即將到來,但不是解密。爲什麼和在哪裏失敗
錯字的問題是題外話,不利於未來的讀者。您可以刪除您的問題,也可以等到它關閉然後自動刪除。 – 2015-02-05 20:27:40