为什么 Laravel 需要消息验证码 (MAC) 来加密?

Why does Laravel need a message authentication code (MAC) for it's Encryption?

Laravel documentation

All of Laravel's encrypted values are signed using a message authentication code (MAC) so that their underlying value can not be modified once encrypted.

在实践中,这意味着有效载荷伴随着一个小的哈希值。这个值是如何产生的并不是秘密,因为 Laravel 是一个开源产品。源代码是这样说的:

    // Once we get the encrypted value we'll go ahead and base64_encode the input
    // vector and create the MAC for the encrypted value so we can then verify
    // its authenticity. Then, we'll JSON the data into the "payload" array.
    $mac = $this->hash($iv  = base64_encode($iv), $value);

我个人认为 MAC 对 Laravel 没有好处。为什么它在那里?

我的意思是,如果我们已经有了 public 与消息一起出现的密钥和隐藏在某处的私钥,并且 openssl_encrypt 作为处理器。 MAC 如何为安全做出贡献?或者它对其他事情有贡献吗?

Laravel 3 中存在一个安全问题,您可以在其中以经过身份验证的用户身份获得访问权限。虽然这似乎与 cookie 更相关(您可以以某种方式伪造它们),但是 MAC 被添加到 cookie 中。

http://joncave.co.uk/2012/10/lying-to-laravel/

TLDR In the future, it would be good to see Laravel’s Crypter class have MACs built in so that all encrypted messages are verified before decryption. Examples of this type of behaviour can be seen in Zend Framework 2 and Ruby on Rails.

https://laravel3.veliovgroup.com/docs/changes#3.2.8

这是因为,解密可以用暴力破解,加一个MAC意味着如果不匹配就什么都不做。 Laravel 的确切实现,我不知道可以增加多少安全性,但至少让攻击者更难。

正如 James K Polk 所说

A MAC 使用密钥,因此攻击者只有拥有密钥才能生成正确的密钥。

需要 MAC 来防止有意修改密文。