| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace LdzfPay\Api\Common;
- use LdzfPay\Api\Common\Logger;
- use LdzfPay\UmfPayService\UmfService;
- use think\Config;
- /**
- * RSA加解密工具类
- */
- class RSACryptUtil {
- /**
- * 使用公钥对明文字符串进行加密
- * @param $data string 明文
- * @return string string 密文
- */
- public static function encrypt($data) {
- // $log = new Logger ();
- //$cert_file = UmfService::$publicKeyPath;
- //$log->logInfo("[UMF SDK]公钥文件绝对路径 ".$cert_file);
- //if(!file_exists($cert_file)){
- // $log->logInfo("[UMF SDK]传入的公钥文件不存在,公钥文件绝对路径 ".$cert_file);
- // die("[UMF SDK]传入的公钥文件不存在,公钥文件绝对路径 ".$cert_file);
- //}
- $cert_file = Config::get('ldys_pay')['public_file'];
- $fp = fopen($cert_file,"r");
- $public_key = fread($fp,8192);
- // $public_key = UMF_PUBLIC_KEY;
- fclose($fp);
- $public_key = openssl_get_publickey($public_key);
- openssl_public_encrypt($data,$crypttext,$public_key);
- $encryptDate = base64_encode($crypttext);
- //$log->logInfo ( "[UMF SDK]敏感信息加密前明文: " . $data);
- // $log->logInfo ( "[UMF SDK]敏感信息加密后密文: " . $encryptDate);
- return $encryptDate;
- }
- /**
- * @param $data string 密文
- * @param $merId string 商户号
- * @return string string 明文
- */
- public function decrypt($data,$merId) {
- // $log = new Logger ();
- $priv_key_file = UmfService::$privateKeyPath[$merId];
- // $log->logInfo("[UMF SDK]私钥文件绝对路径 ".$priv_key_file);
- if(!file_exists($priv_key_file)){
- // $log->logInfo("[UMF SDK]传入的私钥文件不存在,私钥文件绝对路径 ".$priv_key_file);
- die("[UMF SDK]传入的私钥文件不存在,私钥文件绝对路径 ".$priv_key_file);
- }
- $fp = fopen($priv_key_file,"r");
- $private_key = fread($fp,8192);
- fclose ($fp);
- $private_key = openssl_get_privatekey($private_key);
- openssl_private_decrypt(base64_decode($data), $decrypted, $private_key);
- $decryptDate = iconv("GBK", "UTF-8", $decrypted);
- // $log->logInfo ( "[UMF SDK]敏感信息解密前密文: " . $data);
- //$log->logInfo ( "[UMF SDK]敏感信息解密后明文: " . $decryptDate);
- return $decryptDate;
- }
- }
|