php rsa长文加密解密
密钥类型:
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;
}
}
本文来自博客园,作者:tros,转载请注明原文链接:https://www.cnblogs.com/tros/p/18132057
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步