| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- // 使用私钥加密
- class Hqpay
- {
- private $pi_key;
- private $pu_key;
- private $uid;
- private $key;
- public function __construct($merchantID,$platpkey){
- $cfg = require APP_ROOT_PATH."system/payment/Hqpay/config.php";
- $this->pi_key = $cfg['private_key'];
- $this->pu_key = openssl_pkey_get_public($cfg['public_key']);;
- $this->uid = $merchantID;
- $this->key = $platpkey;
- }
- public function privateEncrypt($data)
- {
- openssl_private_encrypt($data, $encrypted, $this->pi_key);
- return base64_encode($encrypted);
- }
- // 使用公钥解密
- public function publicDecrypt($encrypted)
- {
- openssl_public_decrypt($encrypted, $decrypted, $this->pu_key);
- return $decrypted;
- }
- /**
- * RSA验签
- * @param $data 待签名数据
- * @param $sign 要校对的的签名结果
- * return 验证结果
- */
- public function rsaVerify($data, $sign) {
- $pubkey = $this->pu_key;
- $res = openssl_get_publickey($pubkey);
- $result = (bool)openssl_verify($data, base64_decode($sign), $res);
- openssl_free_key($res);
- return $result;
- }
- /**
- * RSA解密
- * @param $str 需要解密的内容,密文
- * @param $private_key_path 商户私钥文件路径
- * return 解密后内容,明文
- */
- public function rsaDecrypt($str) {
- $str = str_replace('_', "+", $str);
- $str = base64_decode($str);
- // 按128位使用平台公钥分段进行解密
- $index = 128;
- $indexx = 0;
- $string = '';
- $strlen = strlen($str);
- $step = $strlen / $index;
- $decrypted = array();
- for ($i = 0; $i < $step; $i ++) {
- $strr = substr($str, $indexx, $index);
- $indexx += $index;
- $decrypted[] .= $this->publicDecrypt($strr);
- }
- return implode('',$decrypted);
- }
- }
|