/**
* 对数据进行加密
* @param [type] $action encrypt/decrypt
* @return [type] string 加密后的字符串
*/
function opensslEncrypt($data)
{
$output = false;
$options = cryptOptions();
$encrypt_method = $options['encrypt_method'];
$secret_key = $options['secret_key']; // This is my secret key
$secret_iv = $options['secret_iv']; // This is my secret iv
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_encrypt($data, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
return $output;
}
/**
* 对数据进行解密
* @param [type] $data 要加密的数据
* @return [type] string 加密后的字符串
*/
function opensslDecrypt($data)
{
$output = false;
$options = cryptOptions();
$encrypt_method = $options['encrypt_method'];
$secret_key = $options['secret_key']; // This is my secret key
$secret_iv = $options['secret_iv']; // This is my secret iv
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_decrypt(base64_decode($data), $encrypt_method, $key, 0, $iv);
return $output;
}
PHP openssl_encrypt_decrypt
最新推荐文章于 2023-02-07 11:24:34 发布