Search code examples
javascriptencryptioncryptojs

Decryption is failing with cryptojs


I have this javascript. When I'm trying to decrypt it is not giving any output. Encrypt is working fine but not decrypt what wrong am I doing.

<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>

Encryption alerts are coming but not decryption. Why and where it is failing


Solution

  • I think you have only a typo in the line var decrypted = decrypted.toString(CryptoJS.enc.Utf8); it should be var decrypted = decrypt.toString(CryptoJS.enc.Utf8).

    After this you are getting a decoderesult and decrypted. But decrypt.iv still returns undefined. Not sure what's wrong here.

    For a demo see below and here at 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);
    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
    
    <div id="output"></div>