define('ICLOD_CERT_PATH',dirname(__FILE__).'/xx_pri.key' ); //私钥文件
define('ICLOD_CERT_PUBLIC_PATH',dirname(__FILE__).'/xx_pub.key' );//公钥文件
class RSA{
/**
公钥用于对数据进行加密,私钥用于对数据进行解密。
私钥用于对数据进行签名,公钥用于对签名进行验证
* @access public
* @param 签名算法
* @param $data
* @return string
*/
public function sign($data){
$priKey = file_get_contents(ICLOD_CERT_PATH);
$res = openssl_get_privatekey($priKey);
openssl_sign($data, $sign, $res);
openssl_free_key($res);
//base64编码
$sign = base64_encode($sign);
return $sign;
}
/**
* @access public
* @param 加密算法
* @param $data
* @return string
*/
public function rsa($data){
$encryptData="";
$priKey = file_get_contents(ICLOD_CERT_PATH);
$res=openssl_get_privatekey($priKey);
$result =openssl_private_encrypt($data, $encryptData, $res,OPENSSL_PKCS1_PADDING);
openssl_free_key($res);
return base64_encode($encryptData);
}
/**
* @access public
* @param 解密算法
* @param $data
* @return string
*/
public function decryptRSA($data){
$decryptData ='';
$publickey =file_get_contents(ICLOD_CERT_PUBLIC_PATH);
$res = openssl_pkey_get_public($publickey);
$result=openssl_public_decrypt(base64_decode($data), $decryptData, $res);
return $decryptData;
}
/**
* @access public
* @param 验签
* @param $data
* @return json
*/
public function verify($data){
$publickey =file_get_contents(ICLOD_CERT_PATH);
$res=openssl_get_publickey($publickey);
$result = (bool)openssl_verify($data['signedValue'], base64_decode($data['sign']), $res);
openssl_free_key($res);
return $result;
}
}
公私钥详情:https://www.cnblogs.com/wt645631686/p/8390936.html