LARAVEL Payload is invalid 错误但解密数据是正确的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48541275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:19:35  来源:igfitidea点击:

LARAVEL Payload is invalid error but decrypted data is correct

phplaravelencryptionpayloadmutators

提问by 180doman

Recently i made new project with composer and added very basic authentication with make:auth - nothing fancy.

最近我用作曲家做了一个新项目,并用 make:auth 添加了非常基本的身份验证 - 没什么特别的。

Next i wanted to encrypt nameand emailcolumns on my database so i changed their types from VARCHAR(191) to LONGTEXT and added some very basic mutators to Users model

接下来我想加密我数据库中的nameemail列,所以我将它们的类型从 VARCHAR(191) 更改为 LONGTEXT 并向用户模型添加了一些非常基本的修改器

public function setNameAttribute($value) {
    $this->attributes['name'] = Crypt::encryptString($value);
}

public function getNameAttribute($value) {
    return Crypt::decryptString($value);
}

public function setEmailAttribute($value) {
    $this->attributes['email'] = Crypt::encryptString($value);
}

public function getEmailAttribute($value) {
    return Crypt::decryptString($value);
}

But when im testing with my very simple route i get Payload is invalid error even though i see in error contents that fields has been decrypted properly.

但是,当我使用我的非常简单的路线进行测试时,即使我在错误内容中看到字段已被正确解密,我也会收到有效载荷无效错误。

Route::get('user',function(){
$user= \App\User::find(3);
//dd($user->name);
dd(Crypt::decryptString($user->name));
dd(Crypt::decryptString($user->email));

});

});

Screenshot link https://ibb.co/deBPpR

截图链接 https://ibb.co/deBPpR

回答by Sapnesh Naik

The error is because you are calling Crypt::decryptStringon an already mutated (and decrypted) name property. i.e your getNameAttributedecrypts the encrypted string and when you call

错误是因为您正在调用Crypt::decryptString一个已经发生变异(和解密)的 name 属性。即你getNameAttribute解密加密的字符串,当你打电话

Crypt::decryptString($user->name); // this is causing the error

You basically pass a decrypted string for decryption again.

您基本上可以再次传递解密的字符串进行解密。

Just do:

做就是了:

echo $user->name;

and you will get the decrypted name.

你会得到解密的名字。

If you really want to see the raw value then use:

如果您真的想查看原始值,请使用:

echo $user->getOriginal('name'); //get original value from DB bypassing the accessor