解密加密消息时cryptojs抛出的异常

时间:2014-12-09 04:21:43

标签: javascript encryption cryptojs

我在尝试解密cryptojs加密邮件时遇到异常。我的解密javascript代码如下。我有点不知道发生了什么。我做了一些调试,可以看到密码核心库中的Base64未定义......

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);    
}

我收到以下异常

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)

我不知道出了什么问题......在cryptojs中出现了一些错误

更新

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

1 个答案:

答案 0 :(得分:10)

我不确定您为什么需要组件脚本,但您应该更改顺序。这是通过反复试验找到的工作顺序:

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

显示代码所需的最小封面集

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

core包含在汇总中,base64包含在aes中。代码中不需要utf16。我不确定cipher-core是做什么的,但我想它也包含在aes中。

这些文件可从Google Code Archive自行托管。