Search code examples
javascriptencryptioncryptojs

Exception thrown by cryptojs while decrypting an encrypted message


I get an exception while trying to decrypt a cryptojs encrypted message. My decryption javascript code is given below. I am kind of stuck with no clue of what is happening. I did some debugging and could see that Base64 within the cipher-core library was undefined...

function  decrypt(){
    var salt = CryptoJS.enc.Hex.parse("3A79D3242F9D0DCE0C811DCCE7F830C5");
    var iv = CryptoJS.enc.Hex.parse("9BCBD77036744C7F26DF591AE6A772C6");
    var encryptedBase64 =   "eKCnyuKiH3lvknsNZq9hARCr6xtDLU/De7sPc3RPSRFAh7WCurBKmDZx/Ol0mbROBtAJBCT0+U927iygd4GspQ==";
    var key = CryptoJS.PBKDF2("passwordA", salt, { keySize: 128/32, iterations: 100 });   
    console.log('key '+key); 
    var encryptedStr = encryptedBase64; //CryptoJS.enc.Base64.parse(encryptedBase64);
    console.log('encryptedStr  : '+ encryptedStr );    
    var decrypted = CryptoJS.AES.decrypt(encryptedStr, key, { iv: iv, mode:        CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });  
    console.log('decrypted : '+ decrypted);    
    var decryptedText = decrypted.toString(CryptoJS.enc.Utf8); 
    console.log('decrypted text : '+ decryptedText);    
}

I get the following exception

TypeError: Cannot read property 'parse'
of undefined at Object.m.OpenSSL.parse(http: //domain.com/PEMSWeb/offlinePageDemo/offlineInspectionApp/CryptoJSv3.1.2/components/cipher-core-min.js:12:101)
at Object.f.SerializableCipher.k.extend._parse(http: //domain.com/PEMSWeb/offlinePageDemo/offlineInspectionApp/CryptoJSv3.1.2/components/cipher-core-min.js:13:220)
at Object.f.SerializableCipher.k.extend.decrypt(http: //domain.com/PEMSWeb/offlinePageDemo/offlineInspectionApp/CryptoJSv3.1.2/components/cipher-core-min.js:13:99)
at Object.decrypt(http: //domain.com/PEMSWeb/offlinePageDemo/offlineInspectionApp/CryptoJSv3.1.2/components/cipher-core-min.js:8:308)

I have no idea what is going wrong ...something is going incorrect within cryptojs

Updated

I have the following libraries in my page in the same sequence
1) CryptoJSv3.1.2/components/core-min.js 
2) CryptoJSv3.1.2/components/cipher-core-min.js 
3) CryptoJSv3.1.2/components/enc-base64-min.js 
4) CryptoJSv3.1.2/components/enc-utf16-min.js 
5) CryptoJSv3.1.2/rollups/aes.js 
6) CryptoJSv3.1.2/rollups/pbkdf2.js

Solution

  • I'm not sure why you need the components scripts, but you should change the order around. This is a working ordering found by trial and error:

    <script src="components/core-min.js "></script>
    <script src="rollups/aes.js"></script>
    <script src="components/cipher-core-min.js"></script>
    <script src="components/enc-base64-min.js"></script>
    <script src="rollups/pbkdf2.js"></script>
    <script src="components/enc-utf16-min.js"></script>
    

    and minimal cover set that is needed for the shown code is

    <script src="rollups/aes.js"></script>
    <script src="rollups/pbkdf2.js"></script>
    

    core is contained in the rollups, base64 is contained in aes. utf16 is not needed in the code. I'm not sure what cipher-core does, but I guess it is also contained in aes.

    The files are available for self-hosting from the Google Code Archive.