无法从工匠命令 Laravel/Lumen 进行 Crypt::decrypt

Posted

技术标签:

【中文标题】无法从工匠命令 Laravel/Lumen 进行 Crypt::decrypt【英文标题】:Couldn't Crypt::decrypt from artisan command Laravel/Lumen 【发布时间】:2016-12-26 02:59:47 【问题描述】:

我正在做一个需要存储加密信息的项目,使用use Illuminate\Support\Facades\Crypt;时,存储、更新、显示(在控制器中使用)工作正常

但是当我需要创建一个按计划运行的命令时,在该命令中我需要获取未加密的代码,但是当我使用它时,它会显示

MAC 无效

注意:当我使用use Crypt;时,Controller中的代码工作正常,但在Command中,它说

找不到类“地穴”

Command中的代码(函数topupSim()导致错误):

public function handle()

    Log::info("** TopUpForCriticalSimsCommand: started");

    $sims = Sim::getCriticalSims();
    foreach ($sims as $sim) 
        $telco_id = $sim->telco_id;

        //Find topup code with same telco
        $topup_code = TopupCode::getAvailableCode($telco_id);

        if ($topup_code) 
            /** @var TopupCode $topup_code */
            if (Engine::topupSim($sim, $topup_code))
                Log::info("** TopUpForCriticalSimsCommand: topup success
                 for $sim->phone_number with code $topup_code->getCensoredCode()");
            else
                Log::info("** TopUpForCriticalSimsCommand: topup fail
                 for $sim->phone_number with code $topup_code->getCensoredCode()");
            
        
    

    Log::info("** TopUpForCriticalSimsCommand: finished");

topupSim 函数,使用 Crypt 的代码:

public static function topupSim(Sim $sim, TopupCode $topupCode)

    $code = $topupCode->getCode();
    $serial = $topupCode->getSerial();

    //TODO: Do topup for sim
    if (false /*Success*/) 

        $topupCode->setUse();

        return true;
    

    return false;

2 个函数getCode()getSerial() 正在使用 Crypt,这 2 个函数在模型中

public function getCode()

    $code = Crypt::decrypt($this->getAttribute('code'));
    return $code;


public function getSerial()

    $serial = Crypt::decrypt($this->getAttribute('serial'));
    return $serial;

日志文件:

[2016-08-19 02:58:57] lumen.INFO: ** TopUpForCriticalSimsCommand: started  
[2016-08-19 03:01:02] lumen.ERROR: exception 'Illuminate\Contracts\Encryption\DecryptException' with message 'The MAC is invalid.' in D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\illuminate\encryption\BaseEncrypter.php:48
Stack trace:
#0 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\illuminate\encryption\Encrypter.php(96): Illuminate\Encryption\BaseEncrypter->getJsonPayload('eyJpdiI6InBFT2d...')
#1 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\illuminate\support\Facades\Facade.php(218): Illuminate\Encryption\Encrypter->decrypt('eyJpdiI6InBFT2d...')
#2 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\app\Models\TopupCode.php(114): Illuminate\Support\Facades\Facade::__callStatic('decrypt', Array)
#3 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\app\Models\TopupCode.php(114): Illuminate\Support\Facades\Crypt::decrypt('eyJpdiI6InBFT2d...')
#4 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\app\Engine.php(721): Ved\SmsGateway\Models\TopupCode->getCode()
#5 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\app\Console\Commands\TopUpForCriticalSimsCommand.php(57): Ved\SmsGateway\Engine::topupSim(Object(Ved\SmsGateway\Models\Sim), Object(Ved\SmsGateway\Models\TopupCode))
#6 [internal function]: Ved\SmsGateway\Console\Commands\TopUpForCriticalSimsCommand->handle()
#7 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\illuminate\container\Container.php(507): call_user_func_array(Array, Array)
#8 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\illuminate\console\Command.php(169): Illuminate\Container\Container->call(Array)
#9 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\symfony\console\Command\Command.php(256): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#10 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\illuminate\console\Command.php(155): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\symfony\console\Application.php(791): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\symfony\console\Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Ved\SmsGateway\Console\Commands\TopUpForCriticalSimsCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\symfony\console\Application.php(117): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\vendor\laravel\lumen-framework\src\Console\Kernel.php(69): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#15 D:\working-tools\xampp\htdocs\VedSmsGateway\source\sms_gateway\artisan(35): Laravel\Lumen\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#16 main  

我试过了:

composer dump-autoload
composer clear-cache

但没有成功

谁知道,请帮忙,提前谢谢

【问题讨论】:

这个帖子可能会带来信息https://***.com/questions/46070732/laravel-decryptexception-the-mac-is-invalid/47926508#47926508 【参考方案1】:

好的,所以我的项目包含两个链接在一起的较小项目(管理站点和 api/后端)

问题是 api/backend 端的 config/app.php 中的 key 与管理站点不同。我在 .env 中更改了 APP_KEY,现在一切正常!

【讨论】:

以上是关于无法从工匠命令 Laravel/Lumen 进行 Crypt::decrypt的主要内容,如果未能解决你的问题,请参考以下文章

Laravel/Lumen - 如何从网站触发工作流程?

laravel lumen 上的 hhvm artisan 命令很慢

Laravel/Lumen - Artisan 控制台中缺少命令

无法为测试设置单独的数据库 - Laravel/Lumen

获取经过身份验证的用户 Laravel/Lumen Passport

Laravel/lumen 5.2 从现有数据库生成迁移表