<?php
namespace Common\Help;
class AesHelper
{
/**
* @param $key
* @param $data
* @return string
* @author lf 2020/7/6
*/
public static function encrypt($key, $data)
{
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$input = self::pkcs5Pad($data, $size);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$iv = '0102030405060708';
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = bin2hex($data);
return $data;
}
/**
* @param $text
* @param $blocksize
* @return string
* @author lf 2020/7/6
*/
public static function pkcs5Pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
/**
* @param $key
* @param $data
* @return false|string
* @author lf 2020/7/6
*/
public static function decrypt($key, $data)
{
$iv = '0102030405060708';
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, hex2bin($data), MCRYPT_MODE_ECB, $iv);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s - 1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
}
可根据需求自行把hex2bin改成base64_decode