Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

javascript - How to decrypt AES with CryptoJS

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...