Laravel加密/解密问题

发布于 2025-02-11 18:54:33 字数 1027 浏览 2 评论 0原文

我正在将用户存储在我的数据库中的第三方服务的凭据。存储时,将它们施加到加密如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2025-02-18 18:54:33

根据您的加密内容,191可能太短了。 255可能是标准的,但是要确保您从未达到极限,最好将列更改为文本列。使用Crypt ::加密使用简单的字符串,一个200字符串的加密长度为568个字符。这是我用Laravel 9测试的结果:

$str = 'a'; 
for($i=0; $i< 200; $i++) { 

    $crypt = Crypt::encrypt($str);
    echo strlen($str)." - ".strlen($crypt)."\n"; 
    $str.='a';
}

以及结果样本:

50 - 288
100 - 372
200 - 568
300 - 740

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:

$str = 'a'; 
for($i=0; $i< 200; $i++) { 

    $crypt = Crypt::encrypt($str);
    echo strlen($str)." - ".strlen($crypt)."\n"; 
    $str.='a';
}

And a sample of the results:

50 - 288
100 - 372
200 - 568
300 - 740
寄人书 2025-02-18 18:54:33

首先确保其加密。这种错误可能是由于将非加密值传递给解密助手函数的原因。

Make sure first it's encrypted. This kind of error can be due to passing a non encrypted value to decrypt helper function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文