🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ if (! function_exists('aes_encode')) { /** * @param $response_array_data * @return string */ function aes_encode($response_array_data) { $key = 'xxx'; $iv = 'xxx'; $response_array_data = json_encode($response_array_data, JSON_UNESCAPED_UNICODE); $encode_data = base64_encode(openssl_encrypt($response_array_data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv)); return $encode_data; } } /** * 接口aes 加密 */ if (! function_exists('aes_decode')) { /** * @param $response_array_data * @return string */ function aes_decode($data) { $key = 'xxx'; $iv = 'xxx'; $decode_data = openssl_decrypt(base64_decode($data), "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv); return $decode_data; } } ~~~ ## js > npm install crypto-js ~~~ import CryptoJS from "crypto-js"; || const CryptoJS = require("crypto-js"); // 十六位十六进制数作为密钥 const SECRET_KEY = CryptoJS.enc.Utf8.parse("7f80d3b5c63b76c2"); // 十六位十六进制数作为密钥偏移量 const SECRET_IV = CryptoJS.enc.Utf8.parse("9c7a36d0d8f8a5c9"); const data = "13172" const encryptText = encrypt(data); console.log("加密", encryptText); const decryptText = decrypt(encryptText); console.log("解密", decryptText); /** * 加密方法 * @param data * @returns {string} */ function encrypt(data) { var encrypted = CryptoJS.AES.encrypt(data, SECRET_KEY, { iv: SECRET_IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted; } /** * 解密方法 * @param data * @returns {string} */ function decrypt(data) { var decrypted = CryptoJS.AES.decrypt(data, SECRET_KEY, { iv: SECRET_IV, padding: CryptoJS.pad.Pkcs7 }).toString(CryptoJS.enc.Utf8); return decrypted; } ~~~