You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Helper/Baofu/BFRSA.php

160 lines
5.5 KiB
PHTML

1 year ago
<?php
namespace App\Helper\Baofu;
use Exception;
use think\Log;
if (!function_exists( 'hex2bin')) {
function hex2bin( $str ) {
$sbin = "";
$len = strlen( $str );
for ( $i = 0; $i < $len; $i += 2 ) {
$sbin .= pack( "H*", substr( $str, $i, 2 ) );
}
return $sbin;
}
}
class BFRSA{
/**
* 读取私钥
* @param type $private_key_path
* @param type $private_pwd
* @return array
*
*/
private static function ReadPrivateKey($private_key_path,$private_pwd){
$pkcs12 = file_get_contents($private_key_path);
$private_key = array();
openssl_pkcs12_read($pkcs12, $private_key, $private_pwd);
Log::LogWirte(empty($private_key) == true ? "读取私钥是否可用:不可用":"读取私钥是否可用:可用");
if(empty($private_key)){
throw new Exception("读取本地私钥异常,请检查证书、密码或路径是否正确");
}
return $private_key["pkey"];
}
/**
* 读取公钥
* @param type $PublicKeyPath
* @return type
*/
private static function ReadPublicKey($PublicKeyPath){
$keyFile = file_get_contents($PublicKeyPath);
$public_key = openssl_get_publickey($keyFile);
Log::LogWirte(empty($public_key) == true ? "读取宝付公钥是否可用:不可用":"读取宝付公钥是否可用:可用");
if(empty($public_key)){
throw new Exception("读取本地公钥异常,请检查证书、密码或路径是否正确");
}
return $public_key;
}
/**
* 私钥加密
* @param type $src
* @param type $private_key_path
* @param type $private_pwd
* @return type
*/
public static function encryptedByPrivateKey($src,$private_key_path,$private_pwd){
$private_key = self::ReadPrivateKey($private_key_path, $private_pwd);
$base64_str = base64_encode($src);
$encrypted = "";
$totalLen = strlen($base64_str);
$encryptPos = 0;
$blockSize=117;
while ($encryptPos < $totalLen){
openssl_private_encrypt(substr($base64_str, $encryptPos, $blockSize), $encryptData, $private_key);
$encrypted .= bin2hex($encryptData);
$encryptPos += $blockSize;
}
return $encrypted;
}
/**
* 公钥解密
* @param type $encrypted
* @param type $Public_Key_Path
* @return type
*/
public static function decryptByPublicKey($encrypted,$Public_Key_Path){
$public_key = self::ReadPublicKey($Public_Key_Path);
$decrypt = "";
$totalLen = strlen($encrypted);
$decryptPos = 0;
$blockSize=256;//分段长度
while ($decryptPos < $totalLen) {
openssl_public_decrypt(hex2bin(substr($encrypted, $decryptPos, $blockSize)), $decryptData, $public_key);
$decrypt .= $decryptData;
$decryptPos += $blockSize;
}
$decrypt=base64_decode($decrypt);
return $decrypt;
}
/**
* 公钥加密
* @param type $Data 加密数据
* @param type $PfxPath 私钥路径
* @param type $PrivateKPASS 私钥密码
* @return type
* @throws Exception
*/
public static function encryptByCERFile($Data,$PublicPath){
try {
if (!function_exists( 'bin2hex')) {
throw new Exception("bin2hex PHP5.4及以上版本支持此函数,也可自行实现!");
}
$public_key = self::ReadPublicKey($PublicPath);
$BASE64EN_DATA = base64_encode($Data);
$EncryptStr = "";
$blockSize=117;//分段长度
$totalLen = strlen($BASE64EN_DATA);
$EncryptSubStarLen = 0;
$EncryptTempData="";
while ($EncryptSubStarLen < $totalLen){
openssl_public_encrypt(substr($BASE64EN_DATA, $EncryptSubStarLen, $blockSize), $EncryptTempData, $public_key);
$EncryptStr .= bin2hex($EncryptTempData);
$EncryptSubStarLen += $blockSize;
}
return $EncryptStr;
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
}
/**
* 私钥解密
* @param type $Data 解密数据
* @param type $PublicPath 解密公钥路径
* @return type
* @throws Exception
*/
public static function decryptByPFXFile($Data,$PfxPath,$PrivateKPASS){
try {
if (!function_exists( 'hex2bin')) {
throw new Exception("hex2bin PHP5.4及以上版本支持此函数,也可自行实现!");
}
$KeyObj = self::ReadPrivateKey($PfxPath,$PrivateKPASS);
$DecryptRsult="";
$blockSize=256;//分段长度
$totalLen = strlen($Data);
$EncryptSubStarLen = 0;
$DecryptTempData="";
while ($EncryptSubStarLen < $totalLen) {
openssl_private_decrypt(hex2bin(substr($Data, $EncryptSubStarLen, $blockSize)), $DecryptTempData, $KeyObj);
$DecryptRsult .= $DecryptTempData;
$EncryptSubStarLen += $blockSize;
}
return base64_decode($DecryptRsult);
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
}
}
?>