POST,GET数据传输加密函数,防止坏蛋通过截传值搞事情。
/**
* 加密解密函数
* @param type $_str 字符串
* @param type $type encode加密 decode 解密
*/
function RSA_openssl($_str, $type = 'encode')
{
if ($type == 'encode') {
$pu_key = openssl_pkey_get_public(RSAPublic); //验证公钥是否可用
if (!$pu_key) {
die('秘钥已被破坏,请重新获取');
}
openssl_public_encrypt($_str, $encrypted, $pu_key);
$encrypted = base64_encode($encrypted);
}
if ($type == 'decode') {
$pri_key = openssl_pkey_get_private(RSAprivate);
if (!$pri_key) {
die('秘钥已被破坏,请重新获取');
}
openssl_private_decrypt(base64_decode($_str), $encrypted, $pri_key);
}
return $encrypted;
}
使用场景:
1、给php打openssl模块
2、获取到公钥和私钥,函数中的RSAPublic和RSAprivate就是公钥的常量