class RsaEncrypt{ private $_privateKey = false; private $_publicKey = false; /** * 私钥解密 * @param $data 需解密的数据 * @return string 解密后的数据 */ public function __construct() { //header('Content-Type:text/html;charset=utf-8'); } /** * @param $content * @return string * 解析字符串 */ public function decryptByPrivate($content){ $rsa_bit = 2048; $one = $rsa_bit/8; $encrypted = hex2bin($content); $decrytped = ''; $num = ceil(strlen($encrypted) / $one); for($i = 0; $i < $num; $i++){ $data_part = substr($encrypted,$i * $one,$one); $de = ''; if(openssl_private_decrypt($data_part,$de,$this->getPrivateKey())){ $decrytped .=$de; }else{ print_r(openssl_error_string());exit; }; } return $decrytped; } /** * 公钥解密 ** @param $data 需解密的数据 * @return string 解密后的数据 */ public function decryptByPublic($data){ $decrypted = ''; openssl_public_decrypt($data,$decrypted,$this->getPublicKey()); openssl_free_key($this->_publicKey); return $decrypted; } /** * 银联公钥解密 ** @param $data 需解密的数据 * @return string 解密后的数据 */ public function decryptByYinlianPublic($data){ $decrypted = ''; openssl_public_decrypt($data,$decrypted,$this->getYinlianPublicKey()); return $decrypted; } /** * 私钥加密 * @param $data 需加密的数据 * @return string 加密后的数据 */ public function encryptionByPrivate($data){ $crypto = ''; $data = json_encode($data); foreach (str_split($data, 117) as $chunk) { openssl_private_encrypt($chunk, $encryptData, $this->getPrivateKey()); $crypto .= $encryptData; } openssl_free_key($this->_privateKey); return bin2hex($crypto); } /** * 公钥加密 * @param $data 需加密的数据 * @return string 加密后的数据 */ public function encryptionByPublic($data){ $crypto = ''; $data = json_encode($data); foreach (str_split($data, 117) as $chunk) { openssl_public_encrypt($chunk,$encryptData, $this->getPublicKey()); $crypto .= $encryptData; } return bin2hex($crypto); } public function encryptionByYinlianPublic($data){ $crypto = ''; $data = json_encode($data); foreach (str_split($data, 117) as $chunk) { openssl_public_encrypt($chunk, $encryptData, $this->getYinlianPublicKey()); $crypto .= $encryptData; } return bin2hex($crypto); } /** * 获取私钥文件数据 * @return bool|resource */ private function getPrivateKey(){ if($this->_privateKey == false ){ $key = file_get_contents(dirname(__FILE__) . '/pem/private_key.pem'); $this->_privateKey = openssl_pkey_get_private($key); } return $this->_privateKey; } /** * 获取公钥文件数据* @return bool|resource */private function getPublicKey(){if($this->_publicKey == false){$key = file_get_contents(dirname(__FILE__) . '/pem/public_key.pem');$this->_publicKey = openssl_pkey_get_public($key);}return $this->_publicKey;}/** * 获取银联公钥* @return resource */private function getYinlianPublicKey(){$key = file_get_contents((dirname(__FILE__).'/pem/yinlian_public_key.pem'));$public_key = openssl_pkey_get_public($key);return $public_key;}public function sign($data){$method = 'SHA256';openssl_sign($data,$sign,$this->getPrivateKey(),$method);return $sign;}/** * 验签* @param $content * @return mixed */public function verify($content){$method = 'SHA256';if(!openssl_verify($data,$content, $content,$this->getPublicKey(),$method)){$error = openssl_error_string();var_dump($error);}else{ var_dump($data);}return $data;}}
来源:https://www.cnblogs.com/yudapeng/p/11649643.html