Laravel - DecryptException: 'MAC 无效'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46070732/
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
Laravel - DecryptException: 'The MAC is invalid'
提问by user7346035
In laravel for registration I'm using encrypt algorithm for password instead of inbuilt bcrypt function in Laravel because to get password and send it to mail when password is forgot.
在用于注册的 Laravel 中,我使用加密算法作为密码而不是 Laravel 中的内置 bcrypt 函数,因为在忘记密码时获取密码并将其发送到邮件。
But decrypt it is showing a error like
但解密它显示一个错误,如
DecryptException The MAC is invalid in Encrypter.php (line 184)
This , when I run this code it is working on local but server itself it is not working below i have mentioned the code , can anyone please help
这个,当我运行这段代码时,它在本地运行,但服务器本身在下面不起作用,我已经提到了代码,任何人都可以帮忙
public function forgotpassword(Request $request)
{
$email=$request->email;
$selectemail = User::select('email','password','name')
->where('email',$email)
->first();
if($selectemail)
{
$password=decrypt($selectemail->password);
$data = array( 'email' => $selectemail->email,'password' => $password , 'name' => $selectemail->name);
Mail::send('email.resetpassword',$data,function($message) use ($email)
{
$message->to([$email])->subject('Forgot Password Letgo');
});
echo "Mail has sent successfully";
} else {
echo "This email is not yet registered";
}
}
回答by Troyer
The problem is you generated a new APP_KEY, then if you try to decrypt the old encrypted data it will show the DecryptException: The MAC is invalid
.
问题是您生成了一个新的 APP_KEY,然后如果您尝试解密旧的加密数据,它将显示DecryptException: The MAC is invalid
.
If you want to decrypt the old data you need to restore your old APP_KEY.
如果要解密旧数据,则需要恢复旧的 APP_KEY。
After realizing that, now, adding a new problem there, if you stored new data with another APP_KEY or another encryption method you have a problem on the data because they are mixed on the table.
在意识到这一点之后,现在,在那里添加一个新问题,如果您使用另一个 APP_KEY 或其他加密方法存储新数据,那么您的数据就会出现问题,因为它们在表上混合。
In case you don't know when do you started with the new encrypt method or differentiate the new encrypted entries, the fastest solution would be reset all the passwords with the new encrypt method.
如果您不知道什么时候开始使用新的加密方法或区分新的加密条目,最快的解决方案是使用新的加密方法重置所有密码。
You can learn more about how Laravel encryption works on the official Laravel docs.
您可以在官方Laravel 文档 中了解有关 Laravel 加密如何工作的更多信息。
回答by 4givN
To avoid this, use a custom key instead. The default key is APP_KEY, but you can provide one so your decrypt is not linked with new or old APP_KEY. I use the following code to resolve it, and it worked in different APP_KEYs.
为避免这种情况,请改用自定义密钥。默认密钥是 APP_KEY,但您可以提供一个,这样您的解密就不会与新的或旧的 APP_KEY 相关联。我使用以下代码来解决它,它在不同的 APP_KEY 中工作。
function customCrypt($vWord){
$customKey = "blabla_key_with_correct_length";
$newEncrypter = new \Illuminate\Encryption\Encrypter( $customKey, Config::get( 'app.cipher' ) );
return $newEncrypter->encrypt( $vWord );
}
function customDecrypt($vWord){
$customKey = "blabla_key_with_correct_length";
$newEncrypter = new \Illuminate\Encryption\Encrypter( $customKey, Config::get( 'app.cipher' ) );
return $newEncrypter->decrypt( $vWord );
}
Important for key length: if $cipher == 'AES-128-CBC' use $length === 16, if $cipher == 'AES-256-CBC' use $length === 32). Check in config/app.cipher
which cipher your app uses.
密钥长度很重要:如果 $cipher == 'AES-128-CBC' 使用 $length === 16,如果 $cipher == 'AES-256-CBC' 使用 $length === 32)。检查config/app.cipher
您的应用使用的密码。
回答by Kamaro Lambert
I copied the APP_KEY
from the environment it was working devto the productionand the issue was solved. you may want to try it.
我APP_KEY
从它正在开发的环境中复制到生产环境,问题就解决了。你可能想尝试一下。
回答by Amitesh
App key matters in encryption and decryption. I was having 2 sub domains with different projects in which I was encrypting value on sub domain and 1 and trying to decrypt on sub domain 2. Issue was resolved when both projects were having same appkey. Note: No projects should have same appkey!!!
应用密钥在加密和解密中很重要。我有 2 个不同项目的子域,其中我在子域和 1 上加密值并尝试在子域 2 上解密。当两个项目具有相同的 appkey 时,问题得到解决。注意:任何项目都不应该有相同的 appkey!!!
If you have imported DB form one environment to another, most likely you will face this error. Its recommended to have same APP_KEY as data source application in order to fix bug.
如果您已将数据库从一个环境导入到另一个环境,您很可能会遇到此错误。建议使用与数据源应用程序相同的 APP_KEY 以修复错误。