【问题标题】:Decrypt with different key in Laravel在 Laravel 中使用不同的密钥解密
【发布时间】:2019-12-16 10:40:30
【问题描述】:

我正在使用 Laravel 解密从另一个应用程序(也在 Laravel 中)加密的字符串,但一开始我遇到了问题。

我以这种方式从\Illuminate\Encryption\Encrypter 类创建了一个新对象,以便使用不同的键而不是默认键:

$new_encypter = new \Illuminate\Encryption\Encrypter("base64:ABCDEFGHIJKLF=", config('app.cipher'));

但我有这个错误:

唯一受支持的密码是具有正确密钥长度的 AES-128-CBC 和 AES-256-CBC。

我使用的密钥是有效密钥,因为它来自另一个可以正常工作并使用相同加密配置的 Laravel 应用程序。

传递给构造函数的密码是正确的,因为在异常跟踪中有这行代码:

Illuminate\Encryption\Encrypter::__construct("base64:ABCDEFGHIJKLF=", "AES-256-CBC")

哪里出错了?

我正在使用 Laravel 6。

【问题讨论】:

    标签: php laravel encryption


    【解决方案1】:

    密钥采用 base64 编码并以 base64: 为前缀。您必须删除前缀并对其进行 base64 解码。

    EncryptionServiceProvider 就是这样做的:

    // get the app config
    $config = $app->make('config')->get('app');
    
    // see if the key starts with 'base64:'
    if (Str::startsWith($key = $this->key($config), 'base64:')) {
        // decode the key
        $key = base64_decode(substr($key, 7));
    }
    
    return new Encrypter($key, $config['cipher']);
    

    $this->key() 只是从配置数组中检索key 键。

    【讨论】:

    • 感谢@lagbox,这就是解决方案。你拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2019-11-13
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多