尝试在整个数据库中实施加密

Trying to implement encryption across entire database

我有一个相当大的网络应用程序,它目前不加密它在 mysql 数据库中存储的任何数据(密码和一些令牌除外)。有相当多的项目在数据库中存储和检索。我想知道是否有某种方法可以自动加密存储到数据库中的所有内容,然后在从数据库中检索到任何内容时自动解密。我现在最好的选择是将其放入我的 routes.php 文件中:

View::composer('Crypt', function($view){
    $view->with('Crypt', Crypt::class);
});

然后每次网络应用程序在每个控制器、模型和视图中调用数据库时手动更改,例如更改:

$user->email = Input::get('email');

$user->email = Crypt::encode(Input::get('email));

然后每次我检索数据库值(很多)时:

{{Crypt::decode($user->email)}}

我觉得一定有比手动 1000 次更简单的方法,但我不确定怎么做。

为了避免每次调用数据库时都必须重复使用 encrypt()decrypt() 方法,您可以改用 Eloquents Accessors & Mutators.

定义访问器

To define an accessor, create a getFooAttribute method on your model where Foo is the "camel" cased name of the column you wish to access. In this example, we'll define an accessor for the first_name attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of first_name:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Decrypt the user's first name.
     *
     * @param  string  $value
     * @return string
     */
    public function getFirstNameAttribute($value)
    {
        return Crypt::decode($value);
    }
}

定义一个修改器

To define a mutator, define a setFooAttribute method on your model where Foo is the "camel" cased name of the column you wish to access. So, again, let's define a mutator for the first_name attribute. This mutator will be automatically called when we attempt to set the value of the first_name attribute on the model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Encrypt the user's first name.
     *
     * @param  string  $value
     * @return string
     */
    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = Crypt::encode($value);
    }
}

您可以查看 CryptDB

它进行数据库加密,同时允许您仍然对加密数据安全地执行查询。

尽管您需要自己添加对 laravel 的支持。