gpt4 book ai didi

php - Laravel cookie 加密

转载 作者:可可西里 更新时间:2023-11-01 13:27:24 29 4
gpt4 key购买 nike

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

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

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

EncryptCookies 类仅在 Kernel.php 中的 web 部分列出

最佳答案

对于 Laravel Encryption :

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.

对于每次加密,值都是用AES-256 / AES-128加密的,不同的initialization vector用不同的MAC签名,即使你加密相同的值,payload,encrypt的返回值总是不同的。为了更容易理解,您可以查看此示例:

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

第二次尝试:

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

关于php - Laravel cookie 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47793128/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com