org\Crypt::decrypt PHP Method

decrypt() public static method

解密字符串
public static decrypt ( string $value, string $key, string $target = 'url' ) : string
$value string 待加密的数据(数字, 字符串, 数组或对象等)
$key string 解密密钥
$target string 解码目标
return string
    public static function decrypt($value, $key, $target = 'url')
    {
        // Base64解码
        $base = new Base64();
        $value = $base->decode($value, $target);
        // 拆分加密结果(校验码, 有效期, 初始化向量, 加密数据)
        $hmac = substr($value, 0, self::HMAC_SIZE);
        $expire = substr($value, self::HMAC_SIZE, self::EXPIRE_SIZE);
        $iv = substr($value, self::HMAC_SIZE + self::EXPIRE_SIZE, self::IV_SIZE);
        $value = substr($value, self::HMAC_SIZE + self::EXPIRE_SIZE + self::IV_SIZE);
        // 超出有效期
        if (time() > hexdec(bin2hex($expire))) {
            return false;
        }
        // 验证密文是否被篡改
        if (static::compareString(static::hmac($iv, $value, $key), bin2hex($hmac)) === false) {
            return false;
        }
        // 解密数据
        $value = openssl_decrypt($value, self::CIPHER_MODE, $key, OPENSSL_RAW_DATA, $iv);
        if (false === $value) {
            return false;
        }
        // 反序列化
        $value = static::unpacking($value);
        // 返回解密结果
        return $value;
    }