laravel 地穴错误 - “MAC 无效”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30585180/
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 11:41:25  来源:igfitidea点击:

Crypt error - 'MAC is invalid.'

phplaravellaravel-5

提问by NightMICU

A site I am developing includes a messaging system. I am encrypting messages in the table using Crypt::(). A user received a message and this error was displayed:

我正在开发的一个站点包括一个消息传递系统。我正在使用 加密表中的消息Crypt::()。用户收到一条消息并显示此错误:

exception 'Illuminate\Contracts\Encryption\DecryptException' with message 'MAC is invalid.' 
in /home/forge/cvahimt.org/releases/20150601155111/vendor/laravel/framework/src/Illuminate/Encryption/Encrypter.php:147

I am encrypting the message like so:

我像这样加密消息:

// Message
        $message = Message::create(
            [
                'thread_id' => $thread->id,
                'user_id' => Auth::user()->id,
                'body' => Crypt::encrypt($input['message']),
            ]
        );

The app key is set in the .envfile and had not been changed, the message was sent just prior to the error occurring. The bodycolumn in the table is of type TEXT.

应用程序密钥设置在.env文件中并且没有更改,消息是在错误发生之前发送的。body表中的列的类型是TEXT

Any idea what might be causing this?

知道是什么原因造成的吗?

回答by Win

In app.phpyou can see the following line

app.php你可以看到以下行

'key' => env('APP_KEY', 'SomeRandomString')

Sometimes envreturns the 'APP_KEY'(reference: https://laracasts.com/discuss/channels/general-discussion/env-not-reading-variables-sometimes) value as empty, so default value is taken as "SomeRandomString"while encrypting or decrypting. So, you can replace it like

有时env'APP_KEY'(参考:https: //laracasts.com/discuss/channels/general-discussion/env-not-reading-variables-sometimes)值返回为空,因此"SomeRandomString"在加密或解密时采用默认值。所以,你可以像这样替换它

'key' => env('APP_KEY', 'xxxxxxxxxxxxx')

where 'xxxxxxxxxxxxx'exactly matches with .envfile's APP_KEYvalue.

其中'xxxxxxxxxxxxx'.env文件的APP_KEY值完全匹配。

回答by Marcel

Try this:

尝试这个:

composer dump-autoload

composer dump-autoload

composer clear-cache

composer clear-cache

Then, clear any seeds from your database and reseed.

然后,清除数据库中的所有种子并重新播种。

Somewhere in that process, the problem should make itself apparent if there is one.

在这个过程中的某个地方,如果存在问题,问题就会变得明显。