如何解密 cakephp 3 cookie

How to decrypt a cakephp 3 cookie

假设您有一个这样的 cookie 字符串:

Q2FrZQ==.AAAAAAAAAAAABBBBBBBBBBBBBCCCCCCCCCCCCCCDDDDDDDDDDDDD

如何使用 AES 在 cakephp 3 中解密它?

好像Cake\Utility\Security::decrypt($cipher, $key, $hmacSalt = null)做到了:

http://book.cakephp.org/3.0/en/core-libraries/security.html#Cake\Utility\Security::decrypt

但是参数呢? hmacSalt 是应用程序的盐值,但是第二个参数中的 key 值是多少?

看看cookie组件的来源,$hmacSalt参数没有被使用,

https://github.com/cakephp/.../Controller/Component/CookieComponent.php#L437

并且 $key 参数由组件 key 配置选项值提供,其中包含

Encryption key used when encrypted cookies are enabled. Defaults to Security.salt.

因此,除非您手动配置了 cookie 组件 key 选项,否则解密 AES 加密 cookie 值所需的全部内容应该是

Security::decrypt($value, Security::salt());

其中 $value 是正确提取和解码的原始加密数据,因为 cookie 组件会传递它:

$prefix = 'Q2FrZQ==.';
$value = base64_decode(substr($value, strlen($prefix)));

https://github.com/cakephp/.../Controller/Component/CookieComponent.php#L431-L432