如何使用 CryptoJS 解密 AES
一段时间以来,我一直在尝试解密使用 Java 应用程序的 AES 中的消息,但它永远无法正常工作.有人可以帮我吗?
I'm trying for some time to decrypt a message in AES that use a Java app , but it never works . Can someone help me?
var options = { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 };
/*** encrypt */
var json = CryptoJS.AES.encrypt("Message", "KEY", options);
var ciphertext = json.ciphertext.toString(CryptoJS.enc.Base64);
console.log("chiper text ", ciphertext);
/*** decrypt */
var decrypted = CryptoJS.AES.decrypt(json, "KEY", options);
var plaintext = decrypted.toString(CryptoJS.enc.Utf8);
console.log("decrypted ", plaintext);
但它总是生成不同的密文,从我的数据库中永远不会相同.
But it is always generated a different ciphertext, never the same from my database.
推荐答案
var CryptoJS = require("crypto-js");
var key = CryptoJS.enc.Utf8.parse('b75524255a7f54d2726a951bb39204df');
var iv = CryptoJS.enc.Utf8.parse('1583288699248111');
var text = "My Name Is Nghĩa";
var encryptedCP = CryptoJS.AES.encrypt(text, key, { iv: iv });
var decryptedWA = CryptoJS.AES.decrypt(encryptedCP, key, { iv: iv});
var cryptText = encryptedCP.toString();
console.log(cryptText);
console.log(decryptedWA.toString(CryptoJS.enc.Utf8));
//Decode from text
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(cryptText )
});
var decryptedFromText = CryptoJS.AES.decrypt(cipherParams, key, { iv: iv});
console.log(decryptedFromText.toString(CryptoJS.enc.Utf8));
相关文章