## PHP RSA和RSA2加密算法代码
`rsa和rsa2加密代码实现`
[公钥私钥生成地址](http://www.metools.info/code/c80.html)
```javascript
// An highlighted block
1024 为rsa公私钥对
2048 为rsa2公私钥对
pub_key rsa公钥
pri_key rsa私钥
粘贴即可使用
/**
* 有不懂联系邮箱va_tao@163.com
* @param string $decrypted
* @param string $sign_type
* @param string $pub_key
* @return string
* 分段公钥加密
*/
function partPubEncrypt($decrypted='',$sign_type="rsa",$pub_key)
{
$piecewise=$sign_type=='rsa'?117:234;
$dataArray = str_split($decrypted,$piecewise);
$bContent = '';
foreach ($dataArray as $key => $subData) {
$bContent .= publicEncrypt($subData,$pub_key);
}
return base64_encode($bContent);
}
/**
* @param $decrypted
* @param $pub_key
* @return null
* 公钥加密
* pub_key公钥
*/
function publicEncrypt($decrypted,$pub_key)
{
if (!is_string($decrypted)) {
return null;
}
return (openssl_public_encrypt($decrypted, $encrypted,$pub_key)) ? $encrypted : null;
}
/**
* 分段私钥解密
* @param string $encrypted
* @param string $sign_type
* @param string $pri_key
* @return String
*/
function partPrivDecrypt($encrypted='',$sign_type="rsa",$pri_key)
{
$piecewise=$sign_type=='rsa'?128:256;
$encrypted = base64_decode($encrypted);
$dataArray = str_split($encrypted, $piecewise);
$bContent = '';
foreach ($dataArray as $key => $subData) {
$bContent .= privDecryptNB64($subData,$pri_key);
}
return $bContent;
}
/**
* 私钥解密
* @param $encrypted //解密字符
* @param $pri_key 私钥
* @return null
*/
function privDecryptNB64($encrypted,$pri_key)
{
if (!is_string($encrypted)) {
return null;
}
return (openssl_private_decrypt($encrypted, $decrypted, $pri_key)) ? $decrypted : null;
}
```