密钥类型:
1024bit:分段加密字节数为117,分段解密字节数为128。
2048bit:分段加密字节数为245,分段解密字节数为256。
class RsaBill
{
private $public_key_resource;
private $private_key_resource;
public function __construct()
{
$this->public_key_resource = openssl_pkey_get_public(config('app.public_key'));
$this->private_key_resource = openssl_pkey_get_private(config('app.private_key'));
}
//公钥加密
public function public_encrypt(string $source)
{
$maxLength = 245;
$output = '';
while ($source) {
$input = substr($source, 0, $maxLength);
$source = substr($source, $maxLength);
$res = '';
openssl_public_encrypt($input, $res, $this->public_key_resource, OPENSSL_PKCS1_PADDING);
$output .= $res;
}
return base64_encode($output);
}
/**
* 私钥解密
*/
public function private_decrypt(string $input)
{
$maxLength = 256;
$content = base64_decode($input);
$output = '';
while ($content) {
$str = substr($content, 0, $maxLength);
$content = substr($content, $maxLength);
$res = '';
openssl_private_decrypt($str, $res, $this->private_key_resource, OPENSSL_PKCS1_PADDING);
$output .= $res;
}
return $output;
}
}
标签:resource,res,rsa,private,key,maxLength,长文,php,public From: https://www.cnblogs.com/tros/p/18132057