这是我用来加密/解密数据的代码:
// Set the method
$method = 'AES-128-CBC';
// Set the encryption key
$encryption_key = 'myencryptionkey';
// Generet a random initialisation vector
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
// Define the date to be encrypted
$data = "Encrypt me, please!";
var_dump("Before encryption: $data");
// Encrypt the data
$encrypted = openssl_encrypt($data, $method, $encryption_key, 0, $iv);
var_dump("Encrypted: ${encrypted}");
// Append the vector at the end of the encrypted string
$encrypted = $encrypted . ':' . $iv;
// Explode the string using the `:` separator.
$parts = explode(':', $encrypted);
// Decrypt the data
$decrypted = openssl_decrypt($parts[0], $method, $encryption_key, 0, $parts[1]);
var_dump("Decrypted: ${decrypted}");
它ususaly工作正常,但有时(十分之一,甚至更少)它失败了。当它失败时,文本只是部分加密:
发生这是错误消息:
Warning: openssl_decrypt(): IV passed is only 10 bytes long, cipher expects an IV of precisely 16 bytes, padding with \0
当它发生时,加密文本看起来像:
Encrypt me���L�se!
我认为它可能是由PHP中的错误引起的,但我已经在不同的主机上测试过:PHP 7.0.6和PHP 5.6。我还尝试了多个在线PHP解析器,如phpfidle.org或3v4l.org。
似乎openssl_random_pseudo_bytes
并不总是返回一个适当长度的字符串,但我不知道为什么。
以下是示例:https://3v4l.org/RZV8d
继续刷新页面,你会在某个时候得到错误。
答案 0 :(得分:11)
当您生成随机IV时,您会得到raw binary。二进制字符串将包含:
或\0
字符的非零机会,您将其用于将IV与密文分开。这样做会使explode()
给你一个更短的字符串。演示:https://3v4l.org/3ObfJ
简单的解决方案是在此过程中添加base64编码/解码。
那就是please don't roll your own crypto。特别是unauthenticated encryption is dangerous和there are already secure libraries that solve this problem。
不要自己编写,而是考虑使用defuse/php-encryption。这是安全的选择。
答案 1 :(得分:1)
我已经更新了第一篇文章中的代码并将其包装在一个类中。这是基于 Scott Arciszewski 提供的解决方案的固定代码。
class Encryptor
{
/**
* Holds the Encryptor instance
* @var Encryptor
*/
private static $instance;
/**
* @var string
*/
private $method;
/**
* @var string
*/
private $key;
/**
* @var string
*/
private $separator;
/**
* Encryptor constructor.
*/
private function __construct()
{
$app = App::getInstance();
$this->method = $app->getConfig('encryption_method');
$this->key = $app->getConfig('encryption_key');
$this->separator = ':';
}
private function __clone()
{
}
/**
* Returns an instance of the Encryptor class or creates the new instance if the instance is not created yet.
* @return Encryptor
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new Encryptor();
}
return self::$instance;
}
/**
* Generates the initialization vector
* @return string
*/
private function getIv()
{
return openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->method));
}
/**
* @param string $data
* @return string
*/
public function encrypt($data)
{
$iv = $this->getIv();
return base64_encode(openssl_encrypt($data, $this->method, $this->key, 0, $iv) . $this->separator . base64_encode($iv));
}
/**
* @param string $dataAndVector
* @return string
*/
public function decrypt($dataAndVector)
{
$parts = explode($this->separator, base64_decode($dataAndVector));
// $parts[0] = encrypted data
// $parts[1] = initialization vector
return openssl_decrypt($parts[0], $this->method, $this->key, 0, base64_decode($parts[1]));
}
}
$encryptor = Encryptor::getInstance();
$encryptedData = $encryptor->encrypt('Encrypt me please!');
var_dump($encryptedData);
$decryptedData = $encryptor->decrypt($encryptedData);
var_dump($decryptedData);
答案 2 :(得分:0)
这也发生在我身上。我遇到类似的错误
openssl_decrypt(): IV passed is only 14 bytes long, cipher expects an IV of precisely 16 bytes, padding with \0
我使用的是openssl_cipher_iv_length('aes-128-cbc')
之类的小写方法
小写aes- - 方法给出的长度结果在12到16之间变化。Ref:https://www.php.net/manual/en/function.openssl-cipher-iv-length.php
将方法设为大写openssl_cipher_iv_length('AES-128-CBC')
将得到一致的值16。
因此,在加密和解密时,IV长度保持与16相同。