从加密性状迁移到Laravel Model $ cast

发布于 2025-02-13 04:16:23 字数 428 浏览 1 评论 0 原文

我正在尝试迁移以下我用来将DB中的某些列加密到本机Laravel 9加密铸件。

我得到的问题是,尽管它适合新值,但对于现有值,我得到了一些额外的值。

因此,解密后没有获得清洁价值:

Harry Potter

我将得到以下内容:

s:23:"Harry Potter";

我不确定最好的方法是什么?我正在考虑循环浏览所有值并运用一些正则等级,但是这太疯狂了,到目前为止,我已经有很多列和价值。

I'm trying to migrate the following trait that I used to encrypt certain columns in my DB to the native Laravel 9 encrypted cast.

The problem I'm getting is that, although it works great for new values, for existing values I'm getting some extra values.

So instead of getting a clean value after decryption:

Harry Potter

I'm getting the following:

s:23:"Harry Potter";

And I'm not sure what's the best way to proceed? I was thinking about looping through all the values and applying some RegEx, but it's crazy, I have a lot of columns and values by now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

凉宸 2025-02-20 04:16:23

假设您尚未使用两种不同的方法具有混合的加密值:

一种解决方案可能是评论模型中的可加密属性(自定义特征之一),然后编写代码,以重新保存所有具有加密值的模型(这应该保存列解密)。

在这一点上,您可以从项目中删除自定义特征,并在模型中添加Laravel 9加密铸件,并编写代码以重新释放所有内容。

Assuming you don't already have mixed encrypted values with the two different methods :

One solution could be to comment the encryptable attribute in your models(the one of the custom trait) then write code that re-saves all your models that have encrypted values (this should save the columns decrypted).

At this point you'd remove the custom trait from your project and add the Laravel 9 encrypted cast in your models and write code to re-save everything.

瘫痪情歌 2025-02-20 04:16:23

您应该运行迁移以加密值。例如:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;

class EncryptUserEmailInUsersTable extends Migration
{
    public function up()
    {
        DB::table('users')->where('user_email', '!=', null)
            ->select(['id','user_email'])
            ->get()
            ->each(function($z) {
                DB::table('users')->where('id', $z->id)
                    ->update([
                        'user_email' => Crypt::encryptString($z->user_email)
                    ]);
            });
    }
}

You should run a migration to encrypt the values. For example:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;

class EncryptUserEmailInUsersTable extends Migration
{
    public function up()
    {
        DB::table('users')->where('user_email', '!=', null)
            ->select(['id','user_email'])
            ->get()
            ->each(function($z) {
                DB::table('users')->where('id', $z->id)
                    ->update([
                        'user_email' => Crypt::encryptString($z->user_email)
                    ]);
            });
    }
}
月棠 2025-02-20 04:16:23

我猜想当您尝试迁移到本机加密铸件时,它使用了 genterpt() 助手方法。在最新的
尝试与他们俩一起测试,以查看在您的情况下正在使用的。可能与序列化选项不匹配。

ps:这个答案可能会帮助目前正在经历的其他人
由于您的问题可能自2022年以来已解决。

可能已经解决了。

I am guessing when you tried to migrate to the native encrypted cast, it used the encrypt() helper method. In the latest versions, the method used has been updated to Crypt::encryptString.
Try testing with both of them to see which is being used in your case. There might be a mismatch with the serialization options.

PS: This answer may help someone else who is currently experiencing
the issue since yours might have been solved since 2022.

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