Laravel加密/解密问题
我正在将用户存储在我的数据库中的第三方服务的凭据。存储时,将它们施加到加密
如下:
protected $casts = [
'enabled' => 'boolean',
'token_is_valid' => 'boolean',
'service_username' => 'encrypted',
'service_password' => 'encrypted',
'service_practice_pin' => 'encrypted',,
];
然后,我需要使用详细信息在第三方服务中进行身份验证。
我可以从数据库中返回完整模型: dd($ Integration)
,但是,如果我尝试访问Propery dd($ Integration-> service_username)
我收到以下错误:
The payload is invalid. {"userId":12,"exception":"[object] (Illuminate\\Contracts\\Encryption\\DecryptException(code: 0): The payload is invalid. at /var/www/app/vendor/laravel/framework/src/Illuminate/Encryption/Encrypter.php:221)
[stacktrace]
我正在使用的第三方服务我正在使用需要解密的值来验证。
我尝试在模型中添加登录器,但仍然会遇到相同的错误,
public function getDecryptedServiceUsernameAttribute()
{
return decrypt($this->attributes['service_username']);
}
如何接收/发送解密值?我需要以不同的方式存储它吗?当前的列类型为varchar(191)
I am storing users credentials for a 3rd party service in my database. When storing, these are cast to encrypted
as below:
protected $casts = [
'enabled' => 'boolean',
'token_is_valid' => 'boolean',
'service_username' => 'encrypted',
'service_password' => 'encrypted',
'service_practice_pin' => 'encrypted',,
];
I then need to use the details to authenticate with the 3rd party service.
I can return the full model from my database:dd($integration)
, however if I try to access the propery dd($integration->service_username)
I get the below error:
The payload is invalid. {"userId":12,"exception":"[object] (Illuminate\\Contracts\\Encryption\\DecryptException(code: 0): The payload is invalid. at /var/www/app/vendor/laravel/framework/src/Illuminate/Encryption/Encrypter.php:221)
[stacktrace]
The 3rd party service I am using requires the decrypted values to authenticate.
I tried adding an accessor to the model but still get the same error
public function getDecryptedServiceUsernameAttribute()
{
return decrypt($this->attributes['service_username']);
}
How can I receive/send the decrypted value? Do I need to store it differently? current column type is varchar(191)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的加密内容,191可能太短了。 255可能是标准的,但是要确保您从未达到极限,最好将列更改为文本列。使用Crypt ::加密使用简单的字符串,一个200字符串的加密长度为568个字符。这是我用Laravel 9测试的结果:
以及结果样本:
Depending on what you're encrypting, 191 can be too short. 255 is probably standard, but to make sure you never hit the limit, you would be better off changing the column to a text column. Using Crypt::encrypt with a simple string, a 200 character string has the encrypted length of 568 characters. Here is what I tested with Laravel 9:
And a sample of the results:
首先确保其加密。这种错误可能是由于将非加密值传递给解密助手函数的原因。
Make sure first it's encrypted. This kind of error can be due to passing a non encrypted value to decrypt helper function.