function jiami(){
$userIds = input('userid');
$key = input('key');//自定义秘钥例如 001,abc
$userId = $this->encrypt(intval($userIds), $key);
echo $userId;
}
function jiemi(){
$userIds = input('userid');
$key = input('key');//解密秘钥与加密秘钥相同
$userId = $this->decrypt($userIds, $key);
echo $userId;
}
/**
* 实现AES加密
* $str : 要加密的字符串
* $keys : 加密密钥
*/
function encrypt($str = '', $keys = '')
{
$input = json_encode($str);
$data = openssl_encrypt($input, 'AES-256-ECB', $keys, OPENSSL_RAW_DATA);
$encrypted_string = bin2hex($data);
return $encrypted_string;
}
/**
* 实现AES解密
* $str : 要解密的字符串
* $keys : 加密密钥
*/
function decrypt($str = '', $keys = "")
{
$sStr = hex2bin(strtolower($str));
$decrypted = openssl_decrypt($sStr, 'AES-256-ECB', $keys, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s - 1]);
$decrypted = substr($decrypted, 0, - $padding);
return $decrypted;
}
传参数时,可通过多种秘钥进行加密