【问题标题】:what is difference in 'The MAC is invalid' and 'The Payload is invalid' laravel?“MAC 无效”和“有效负载无效”laravel 有什么区别?
【发布时间】:2019-05-27 12:21:40
【问题描述】:

我运行两个 laravel 5.7 应用程序,两者都有不同的应用程序密钥。

 -->app 1 key 1,
 -->app 2 key 2

[这点不太重要]这是两个应用程序(基于用户角色的不同模块。使用不同的应用程序密钥,但访问相同的数据库)。

当使用密钥 1 加密应用程序 1 中的某些数据时,现在使用密钥 1 解密应用程序 1 中的加密数据,工作正常。

但我更改了加密数据字符串(应用程序 1,密钥 1)并尝试使用密钥 1 在应用程序 1 中再次解密,它给出错误“有效负载无效”。我消化了这个。

现在,第二件事是,我使用密钥 1 加密应用程序 1 中的数据,并使用密钥 2 在应用程序 2 中传递此加密数据字符串进行解密,它给出另一个错误“MAC 无效。”。

现在我的问题是为什么 laravel 会给出两个不同的错误?为什么不给出同样的错误,因为我们用另一个应用程序发送另一个数据(意味着这对于第二个应用程序是错误的)。

你能区分一下吗?可能会导致整个安全循环。

谢谢。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    Laravel 使用 base64 编码和解码播放负载。来看看吧:

    /**
     * Get the JSON array from the given payload.
     *
     * @param  string  $payload
     * @return array
     *
     * @throws \Illuminate\Contracts\Encryption\DecryptException
     */
    protected function getJsonPayload($payload)
    {
        $payload = json_decode(base64_decode($payload), true);
    
        // If the payload is not valid JSON or does not have the proper keys set we will
        // assume it is invalid and bail out of the routine since we will not be able
        // to decrypt the given value. We'll also check the MAC for this encryption.
        if (! $this->validPayload($payload)) {
            throw new DecryptException('The payload is invalid.');
        }
    
        if (! $this->validMac($payload)) {
            throw new DecryptException('The MAC is invalid.');
        }
    
        return $payload;
    }
    
    
    /**
     * Verify that the encryption payload is valid.
     *
     * @param  mixed  $payload
     * @return bool
     */
    protected function validPayload($payload)
    {
        return is_array($payload) && isset($payload['iv'], $payload['value'], $payload['mac']) &&
               strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length($this->cipher);
    }
    
    
    /**
     * Determine if the MAC for the given payload is valid.
     *
     * @param  array  $payload
     * @return bool
     */
    protected function validMac(array $payload)
    {
        $calculated = $this->calculateMac($payload, $bytes = random_bytes(16));
    
        return hash_equals(
            hash_hmac('sha256', $payload['mac'], $bytes, true), $calculated
        );
    }
    

    Illuminate/Encryption/Encrypter.php

    正如您所看到的,有一个双重检查,如果您手动修改有效负载,它不一定具有正确的结构并且将返回The payload is invalid

    然后,当有效载荷有效时,它将尝试使用 MAC。 内容不匹配时,会返回The MAC is invalid.

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 2018-08-05
      • 2018-02-22
      • 2012-06-14
      • 2013-05-18
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      相关资源
      最近更新 更多