crypto-js与php aes加密传输

2019-08-16  本文已影响0人  骑着大象去上班

CryptoJS (crypto.js) 为 JavaScript 提供了各种各样的加密算法。目前已支持的算法包括:

github项目地址
https://github.com/brix/crypto-js
文档
https://github.com/brix/crypto-js/blob/master/docs/QuickStartGuide.wiki
源码地址
https://github.com/brix/crypto-js/releases

js端
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./crypto-js-3.1.9-1/crypto-js.js"></script>
    <script type="text/javascript">

        /**加密
         *
         * @param data
         * @returns {string}
         */
        function encrypt(data) {
            let text = JSON.stringify(data);
            text = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text)).toString();
            let key = CryptoJS.enc.Utf8.parse('1234567887654321'); //为了避免补位,直接用16位的秘钥
            let iv = CryptoJS.enc.Utf8.parse('1234567887654321'); //16位初始向量
            let encrypted = CryptoJS.AES.encrypt(text, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.ZeroPadding
            }).toString();
            return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encrypted)).toString();
        }
        
        /**解密
         *
         * @param data
         * @returns {string}
         */
        function decrypt(data) {
            let encrypted = CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(data)).toString();
            let key = CryptoJS.enc.Utf8.parse('1234567887654321'); //为了避免补位,直接用16位的秘钥
            let iv = CryptoJS.enc.Utf8.parse('1234567887654321'); //16位初始向量
            let decrypted = CryptoJS.AES.decrypt(encrypted, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.ZeroPadding
            }).toString(CryptoJS.enc.Utf8);
            return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(decrypted)).toString();
        }


        let data = {"username": "admin", "pwd": "passwd"};
        let encrypt_data = encrypt(data);
        let decrypt_data = decrypt(encrypt_data);
        console.log(decrypt_data);
    </script>
</head>
<body>

</body>
</html>
php端
/**加密
     * @param $data
     * @return string
     */
    function encrypt($data)
    {
        $key = "1234567887654321";//秘钥必须为:8/16/32位
        $iv = "1234567887654321";
        $base64_str = base64_encode(json_encode($data));
        $encrypted = openssl_encrypt($base64_str, "aes-128-cbc", $key, OPENSSL_ZERO_PADDING, $iv);
        return base64_encode($encrypted);
    }


    /**解密
     * @param $data
     * @return mixed
     */
    function decrypt($data)
    {
        $encrypted = base64_decode($data);
        $key = "1234567887654321";//秘钥必须为:8/16/32位
        $iv = "1234567887654321";
        $decrypted = openssl_decrypt($encrypted, 'aes-128-cbc', $key, OPENSSL_ZERO_PADDING, $iv);
        return json_decode(base64_decode($decrypted), true);
    }

$data = ['username'=>'admin','pwd'=>'admin'];  
$encrypt=encrypt($data);
$str=decrypt($encrypt);
var_dump($str);
上一篇下一篇

猜你喜欢

热点阅读