php aes 128 ecb pkcs5padding-掘金

373 人参与 | 时间:2024年06月24日 12:56:46
内容
 public static function hmac($algo, $data, $key, $raw_output = false)
{
if (function_exists('hash_hmac') && !defined('SIMPLE_TEST')) {
return hash_hmac($algo, $data, $key, $raw_output);
}
$algo = strtolower($algo);
$pack = 'H' . strlen($algo('test'));
$size = 64;
$opad = str_repeat(chr(0x5C), $size);
$ipad = str_repeat(chr(0x36), $size);
if (strlen($key) > $size) {
$key = str_pad(pack($pack, $algo($key)), $size, chr(0x00));
} else {
$key = str_pad($key, $size, chr(0x00));
}
for ($i = 0; $i < strlen($key) - 1; $i++) {
$opad[$i] = $opad[$i] ^ $key[$i];
$ipad[$i] = $ipad[$i] ^ $key[$i];
}
$output = $algo($opad . pack($pack, $algo($ipad . $data)));
return ($raw_output) ? pack($pack, $output) : $output;
}
public function encrypt($input, $config)
{
return base64_encode(openssl_encrypt($input, 'AES-128-CBC', $config['data_secret'], OPENSSL_RAW_DATA, $config['data_secret_iv']));
}
// 解密
public function decrypt($input, $config)
{
return openssl_decrypt(base64_decode($input), 'AES-128-CBC', $config['data_secret'], OPENSSL_RAW_DATA, $config['data_secret_iv']);
}