## 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;
}
```