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.
143 lines
4.6 KiB
PHP
143 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Helper\Baofu;
|
|
|
|
use Exception;
|
|
|
|
class Rsa
|
|
{
|
|
/**
|
|
* 读取私钥
|
|
* @param string $privateKeyPath
|
|
* @param string $privatePwd
|
|
* @return array
|
|
*
|
|
*/
|
|
private static function readPrivateKey($privateKeyPath, $privatePwd)
|
|
{
|
|
$pkcs12 = file_get_contents($privateKeyPath);
|
|
$privateKey = [];
|
|
openssl_pkcs12_read($pkcs12, $privateKey, $privatePwd);
|
|
if(empty($privateKey)){
|
|
throw new Exception('读取本地私钥异常,请检查证书、密码或路径是否正确');
|
|
}
|
|
return $privateKey['pkey'];
|
|
}
|
|
|
|
/**
|
|
* 读取公钥
|
|
* @param string $publicKeyPath
|
|
* @return string
|
|
*/
|
|
private static function readPublicKey($publicKeyPath)
|
|
{
|
|
$keyFile = file_get_contents($publicKeyPath);
|
|
$publicKey = openssl_get_publickey($keyFile);
|
|
if(empty($publicKey)){
|
|
throw new Exception('读取本地公钥异常,请检查证书、密码或路径是否正确');
|
|
}
|
|
return $publicKey;
|
|
}
|
|
|
|
/**
|
|
* 私钥加密
|
|
* @param string $src
|
|
* @param string $privateKeyPath
|
|
* @param string $privatePwd
|
|
* @return string
|
|
*/
|
|
public static function encryptedByPrivateKey($src, $privateKeyPath, $privatePwd)
|
|
{
|
|
$privateKey = self::readPrivateKey($privateKeyPath, $privatePwd);
|
|
$base64Str = base64_encode($src);
|
|
$encrypted = '';
|
|
$totalLen = strlen($base64Str);
|
|
$encryptPos = 0;
|
|
$blockSize = 117;
|
|
while ($encryptPos < $totalLen){
|
|
openssl_private_encrypt(substr($base64Str, $encryptPos, $blockSize), $encryptData, $privateKey);
|
|
$encrypted .= bin2hex($encryptData);
|
|
$encryptPos += $blockSize;
|
|
}
|
|
return $encrypted;
|
|
}
|
|
|
|
/**
|
|
* 公钥解密
|
|
* @param string $encrypted
|
|
* @param string $publicKeyPath
|
|
* @return string
|
|
*/
|
|
public static function decryptByPublicKey($encrypted, $publicKeyPath)
|
|
{
|
|
$publicKey = self::readPublicKey($publicKeyPath);
|
|
$decrypt = '';
|
|
$totalLen = strlen($encrypted);
|
|
$decryptPos = 0;
|
|
$blockSize = 256;//分段长度
|
|
while ($decryptPos < $totalLen) {
|
|
openssl_public_decrypt(hex2bin(substr($encrypted, $decryptPos, $blockSize)), $decryptData, $publicKey);
|
|
$decrypt .= $decryptData;
|
|
$decryptPos += $blockSize;
|
|
}
|
|
$decrypt = base64_decode($decrypt);
|
|
return $decrypt;
|
|
}
|
|
|
|
/**
|
|
* 公钥加密
|
|
* @param string $data 加密数据
|
|
* @param string $publicPath 公钥路径
|
|
* @return string
|
|
* @throws Exception
|
|
*/
|
|
public static function encryptByCERFile($data, $publicPath)
|
|
{
|
|
try {
|
|
$publicKey = self::readPublicKey($publicPath);
|
|
$base64Data = base64_encode($data);
|
|
$EncryptStr = '';
|
|
$blockSize = 117;//分段长度
|
|
$totalLen = strlen($base64Data);
|
|
$encryptSubStarLen = 0;
|
|
$encryptTempData = '';
|
|
while ($encryptSubStarLen < $totalLen){
|
|
openssl_public_encrypt(substr($base64Data, $encryptSubStarLen, $blockSize), $encryptTempData, $publicKey);
|
|
$EncryptStr .= bin2hex($encryptTempData);
|
|
$encryptSubStarLen += $blockSize;
|
|
}
|
|
return $EncryptStr;
|
|
} catch (Exception $exc) {
|
|
echo $exc->getTraceAsString();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 私钥解密
|
|
* @param string $data 解密数据
|
|
* @param string $pfxPath 私钥路径
|
|
* @param string $privateKeyPwd 私钥密码
|
|
* @return string
|
|
* @throws Exception
|
|
*/
|
|
public static function decryptByPFXFile($data, $pfxPath, $privateKeyPwd)
|
|
{
|
|
try {
|
|
$keyObj = self::readPrivateKey($pfxPath, $privateKeyPwd);
|
|
$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();
|
|
}
|
|
}
|
|
} |