Laravel cookie 加密

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47793128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:05:56  来源:igfitidea点击:

Laravel cookie encryption

phplaravelencryptioncookies

提问by Norgul

Is Laravel hashing differently request and response cookies?

Laravel 是否以不同的方式散列请求和响应 cookie?

I am using main domain and subdomains, and have set up CORS and CSRF and if I exclude cookies from EncryptCookiesclass I see the same cookies in the response headers and request headers.

我正在使用主域和子域,并设置了 CORS 和 CSRF,如果我从EncryptCookies类中排除 cookie,我会在响应标头和请求标头中看到相同的 cookie。

If I leave them to encrypt however, I am getting different "encryption strings", and don't know if that is acceptable behaviour?

但是,如果我让它们进行加密,则会得到不同的“加密字符串”,并且不知道这是否可以接受?

EncryptCookiesclass is only listed under webpart in Kernel.php

EncryptCookies班级仅列在web部分Kernel.php

采纳答案by Ben

For Laravel Encryption:

对于Laravel 加密

Laravel's encrypter uses OpenSSL to provide AES-256 and AES-128 encryption. You are strongly encouraged to use Laravel's built-in encryption facilities and not attempt to roll your own "home grown" encryption algorithms. 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 的加密器使用 OpenSSL 提供 AES-256 和 AES-128 加密。强烈建议您使用 Laravel 的内置加密工具,不要尝试推出自己的“自产”加密算法。Laravel 的所有加密值都使用消息验证码 (MAC) 进行签名,因此它们的底层值一旦加密就无法修改。

For each encryption, the value are encrypted with AES-256 / AES-128with different initialization vectorand signed with different MAC, even if you encrypt the same value, the payload, returned value of encryptalways different. For easier understanding, you can check this example:

对于每次加密,该值都使用不同初始化向量的AES-256 / AES-128进行加密,并使用不同的 MAC 进行签名,即使您加密相同的值,有效载荷、返回值也始终不同。为了更容易理解,您可以查看此示例:encrypt

$value = Crypt::encrypt('foo');
// eyJpdiI6ImVoNEVlVWpnYUdwZ1JHRlJWSGlTZEE9PSIsInZhbHVlIjoiVThpWjJNWVBqZnVsWjhLVWNDXC85VHc9PSIsIm1hYyI6IjFjMDRhOTM5ZThhOWRmYjk3Mzk0OWFmNTM3YWE1NDAzNzMxNWY5YTJmODMwNmQxZDE4NDllZGJkMjc1Y2I3ZmYifQ==
base64_decode($value);
// {"iv":"eh4EeUjgaGpgRGFRVHiSdA==","value":"U8iZ2MYPjfulZ8KUcC\/9Tw==","mac":"1c04a939e8a9dfb973949af537aa54037315f9a2f8306d1d1849edbd275cb7ff"}

The second attempt:

第二次尝试:

$value = Crypt::encrypt('foo');
// eyJpdiI6Ill5MmZleG5ycTBaZmQ5NnRDT3N3dVE9PSIsInZhbHVlIjoiTmgrRnlqajJjUk9qTk1qeHJLU21LUT09IiwibWFjIjoiNWEzZDRjZWMwMjg0ZDhlMjhlZWRiODg3ZWQ5MTcxN2I5N2JjY2ZmMzc0NTYyOTI5MThmOTk4YjAyZjM1YTRjMyJ9
base64_decode($value);
// {"iv":"Yy2fexnrq0Zfd96tCOswuQ==","value":"Nh+Fyjj2cROjNMjxrKSmKQ==","mac":"5a3d4cec0284d8e28eedb887ed91717b97bccff37456292918f998b02f35a4c3"}