Agreed way:
aes-128-ecb mode, pkcs7 padding, base64
Problems encountered:
When key
is less than or equal to 16 bits, calling openssl_encrypt($text, 'aes-128-ecb', $key)
has the same result, and when key
is greater than 16 bits, the result is inconsistent.
Solve the problem:
When key
is greater than 16 bits, use 256-bit encryption instead.
<?php
class Encrypter
{
public static function encrypt($key, $text)
{
if (strlen($key) <= 16) {
$algo = 'AES-128-ECB';
} else {
$algo = 'AES-256-ECB';
}
$data = openssl_encrypt($text, $algo, $key);
return $data;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。