Table Of Contents

骑驴找蚂蚁

全干工程师

将mcrypt加密方式换成openssl加密方式

    /**
     * @param $content
     * @param $secret
     * @param string $iv
     * @return string
     */
    public static function aesEncode($content, $secret, $iv = self::DEFAULT_AES_IV)
    {
        $method = self::getAesEncryptMethod($secret);
        $value = openssl_encrypt($content, $method, $secret, OPENSSL_RAW_DATA, $iv);
        return base64_encode($value);
    }

    /**
     * @param $content
     * @param $secret
     * @param string $iv
     * @return string
     */
    public static function aesDecode($content, $secret, $iv = self::DEFAULT_AES_IV)
    {
        $method = self::getAesEncryptMethod($secret);
        return openssl_decrypt(base64_decode($content), $method, $secret, OPENSSL_RAW_DATA, $iv);
    }

    /**
     * @param string $secret
     * @return string
     */
    public static function getAesEncryptMethod(string $secret)
    {
        $len = strlen($secret);
        if ($len <= 16) {
            $method = 'AES-128-CBC';
        } elseif ($len > 16 && $len <= 24) {
            $method = 'AES-192-CBC';
        } else {
            $method = 'AES-256-CBC';
        }
        return $method;
    }

留言