AES 前后端加密解密详解

后台语言PHP aes加密解密


    /**向量
     * @var string
     */
    private static $iv = "";//16位
    /**
     * 默认秘钥
     */
    const KEY = '';//16位
    // const KEY = '';//16位
    /**
     * 加密字符串
     * @param string $data 字符串
     * @param string $key  加密key
     * @return string
     */
    public static function encrypt($data = '', $key = self::KEY)
    {
        $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, self::$iv);
        return base64_encode($encrypted);
    }

    /**
     * 解密字符串
     * @param string $data 字符串
     * @param string $key  加密key
     * @return string
     */
    public static function decrypt($data = '', $key = self::KEY)
    {
        $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($data), MCRYPT_MODE_CBC, self::$iv);
        return rtrim($decrypted, "\0");
    }


前端语言aes加密解密规则

    function encrypt(content) {//加密
        var key = CryptoJS.enc.Latin1.parse('');
        var iv = CryptoJS.enc.Latin1.parse('');
        var encrypted = CryptoJS.AES.encrypt(content, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.ZeroPadding
        });
        return encrypted.toString()
    }
    function decrypt(content) {//解密
        var key = CryptoJS.enc.Latin1.parse('');
        var iv = CryptoJS.enc.Latin1.parse('');
        var bytes = CryptoJS.AES.decrypt(content.toString(), key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.ZeroPadding
        });
        var decryptResult = CryptoJS.enc.Utf8.stringify(bytes).toString();
        return decryptResult
    }

 

你可能感兴趣的:(PHP)