/**
* rsa_encode rsa_decode post请求
* @param 请求参数
*/
用此方法必须开启 php 扩展
公钥 私钥 必须是2048位 私钥加密 公钥解密
公钥 私钥 写在配置文件中 或读取文件
备注 :此循环加密是参数过长
function rsa_encode($data) {
//openssl_pkey_get_private 此方法测试私钥是否可用
$pi_key = openssl_pkey_get_private(Config::get('pri_key'));
//分割加密
$crypto = '';
$encryptData="";
foreach (str_split($data, 245) as $chunk) {
openssl_private_encrypt($chunk, $encryptData,$pi_key);
$crypto .= $encryptData;
}
return base64_encode($crypto);
// //加密之后存入的变量
// $encrypted = "";
// //私钥加密 参数1:是需要传递的参数json 参数2:用于接收加密后存入的变量,参数3:公钥或者私钥
// openssl_private_encrypt($data,$encrypted,$pi_key);
// //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
// $encrypted = base64_encode($encrypted);
// return $encrypted;
}
//解密
function rsa_decode($data) {
//openssl_pkey_get_public 此方法测试公钥是否可用
$pu_key = openssl_pkey_get_public(Config::get('pub_key'));
//分割解密
$crypto = '';
$decryptData="";
foreach (str_split(base64_decode($data), 256) as $chunk) {
openssl_public_decrypt($chunk, $decryptData, $pu_key);
$crypto .= $decryptData;
}
return $crypto;
// //解密之后存入的变量
// $decrypted = "";
// //解密
// openssl_public_decrypt(base64_decode($data),$decrypted,$pu_key);
// return $decrypted;
}