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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27371389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:24:07  来源:igfitidea点击:

Exception thrown by cryptojs while decrypting an encrypted message

javascriptencryptioncryptojs

提问by user1455719

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

尝试解密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);    
}

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

我不知道出了什么问题……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

回答by Artjom B.

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>

coreis contained in the rollups, base64is contained in aes. utf16 is not needed in the code. I'm not sure what cipher-coredoes, but I guess it is also contained in aes.

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

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

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