Laravel 5.3 & Crypt - 有效载荷无效

Laravel 5.3 & Crypt - invalid payload

我有一个在 Laravel 5.23 中编写的应用程序。在其中,我在使用 table 存储之前加密某些敏感的用户数据:

'Crypt'     => Illuminate\Support\Facades\Crypt::class,

该字段的迁移是:

$table->string('field_account_name')->nullable();

我在 class 中设置了修改器来执行此操作:

public function getFieldAccountNameClearAttribute($value)
{
    if ($this->field_account_name) {
        try {
            return Crypt::decrypt($this->field_account_name);
        } catch (DecryptException $e) {
            return 'INVALID';
        }
    } else {
        return $this->field_account_name;
    }
}

public function setFieldAccountNameClearAttribute($value)
{
    $this->attributes['field_account_name'] = Crypt::encrypt($value);
}

这已经运行了很长时间,但用户在一周前放弃了他们的魔法并触发了:

Exception 'ErrorException' with message 'The payload is invalid

我通过在字段中放置特殊字符(例如 ')作为带空格的长字符串的一部分成功地重现了这个问题。

日期已加密存储,但检索失败。

是否有我应该从前端加密中排除的字符以避免这种情况?我找不到这个问题的答案。

或者字段应该是 text 而不是字符串?

此问题可能与您尝试加密的数据量及其在数据库中的存储方式有关。如果加密文本超过255个字符,在添加到数据库时会被截断,这意味着它无法解密。

您应该将现有迁移更新为:

$table->text('field_account_name')->nullable();

或在新迁移中使用以下内容:

$table->text('field_account_name')->nullable()->change();