首页 > 解决方案 > 在 Laravel 中加密时如何为不同的用户使用不同的密钥?

问题描述

我尝试在 Laravel5.7 中加密文件

$encryptedContent = encrypt($fileContent);

用于加密文件。

$decryptedContent = base64_encode(decrypt($encryptedContent));

为解密工作。

我的问题是我需要为不同的用户使用不同的密钥来加密文件和解密。我尝试了以下方法。

$crypt = new \Illuminate\Encryption\Encrypter($newkey);
$encryptedContent = $crypt->encrypt($fileContent);

但它给出了以下错误。

The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths

谁能帮帮我吗?谢谢。

标签: encryptionlaravel-5.7

解决方案


问题在于密钥长度。

如果我们使用AES-128-CBCkey应该是16个字符长度和AES-256-CBC32个字符长度。


推荐阅读