Created
June 5, 2020 06:49
-
-
Save imzhi/71f3b5f7806b40e569cc5d361e9c3795 to your computer and use it in GitHub Desktop.
CryptoJS 实现 Laravel 6.x decrypt 方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * CryptoJS 实现 Laravel 6.x decrypt 方法 | |
| * | |
| * @param encryptStr 加密返回的内容 | |
| * @param appKey .env中的APP_KEY | |
| */ | |
| var laravelDecrypt = function(encryptStr, appKey) { | |
| var parseBase64 = CryptoJS.enc.Base64.parse(encryptStr); | |
| var decoded = parseBase64.toString(CryptoJS.enc.Utf8); | |
| var parseJson = JSON.parse(decoded); | |
| var iv = CryptoJS.enc.Base64.parse(parseJson.iv); | |
| var key = CryptoJS.enc.Base64.parse(appKey.slice(7)); | |
| var decrypted = CryptoJS.AES.decrypt(parseJson.value, key, { | |
| iv: iv, | |
| }); | |
| var result = decrypted.toString(CryptoJS.enc.Utf8); | |
| return result; | |
| }; | |
| // 测试: | |
| var encryptStr = "eyJpdiI6IkRyTzlwWnJzQllha3dhXC9GNXdWVGt3PT0iLCJ2YWx1ZSI6Ik1xekhMOVVJS1dyeE5YS2hKSlphNXc9PSIsIm1hYyI6ImU0NDFiMzE1MWRmODQ5MjA5YzAxY2Q2ODQ1YTcxYzI5MzQxNzhkYmQwMjlhZWI0MTJkZWRlODMwZTJmZjY1ZDcifQ=="; | |
| var appKey = 'base64:ZgyAPu8+XIE8eelQHcbtgV8KVq+gagdOyWiIU0nn10Q='; | |
| var decryptStr = laravelDecrypt(encryptStr, appKey); | |
| console.log(decryptStr); | |
| // 输出:爱老虎勇 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CryptoJS 官方文档:https://cryptojs.gitbook.io/docs/