基于PHP RSA密文过长加密解密 越过1024的解决方法
作者:httpp886 时间:2023-09-07 02:57:56
如下所示:
<?php
namespace helpers;
class OpensslRSA{
//echo $private_key 私钥;
public $private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC+L7ENzBHxKOqjuFHPFKlAt40BatVZhUAHw/G05XshpTGqm9Rv
8wG0EAbFbdo9PuB8DiXdPQLyIfaqkTgpsPjJ1Ow7WKxmYbqZN5IW/GN+TyFWP+MB
2W6IBLPWBDvAl2NJlmU8j3LCPJW1dH4zP1OitkxZMyUuUYYfPuOtO9RJ2wIDAQAB
AoGAUFCbmJQBT7JTxGfmRGkZQLdC2MJg7rkS3TSmMhpm8UJtwvqjr9MTeRL7iQxn
CU4wRrNC0jcds1sca9N/wDt4FCkCala+bg7mwQuPpg5QhXelfFr88ibRnP8y8LmZ
7PPNqx9c4jivhMzJrzNh3luqg6awjsig2w3+EW1/Ubb30AECQQDshTvyc5mpDgiO
4g8q1ztszszL9eCp+IjlUaN51vC3Nj1eXpjbtdSZ0JVKrDdhKcd3rEZVYzMQN/lI
pyq85e/bAkEAzdmN6TF3Y1h3LouumCy6+61ChTFrl/yjw13CGApmAQHhEVyANHr7
NjoxP06eimzn7KHff/eYxd1Emf1SYA8uAQJBAN1ibFUpLRgXAZ20LNw9r+rNutXi
ZJLUBlcXTjv6G0ByLYkKZGuqy7/ZhBPsFL4GnCUBBKhh/ObebaA6kH9VfmcCQGfg
0WxMOiM4EWy7sG+6ouE+ncL5HYKlSz7boYbgOHlpqVpJg6j4Jq1G0HNSCU9xhdg0
F8VL/RxcfLH41AkFoAECQQCR8NDB3BgHqyJfarKKMWQ3qrXHaLfBKExMrpQ8MDzs
MlSBzFOnucufo110lSgjdRlgr8smtU2hx9gXFIqxvfWF
-----END RSA PRIVATE KEY-----';
//公钥
public $public_key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+L7ENzBHxKOqjuFHPFKlAt40B
atVZhUAHw/G05XshpTGqm9Rv8wG0EAbFbdo9PuB8DiXdPQLyIfaqkTgpsPjJ1Ow7
WKxmYbqZN5IW/GN+TyFWP+MB2W6IBLPWBDvAl2NJlmU8j3LCPJW1dH4zP1OitkxZ
MyUuUYYfPuOtO9RJ2wIDAQAB
-----END PUBLIC KEY-----';
public $pi_key;
public $pu_key;
//判断公钥和私钥是否可用
public function __construct()
{
$this->pi_key = openssl_pkey_get_private($this->private_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
$this->pu_key = openssl_pkey_get_public($this->public_key);//这个函数可用来判断公钥是否是可用的
// print_r($this->pi_key);echo "\n";
// print_r($this->pu_key);echo "\n";
}
//私钥加密
/*public function PrivateEncrypt($data){
openssl_private_encrypt($data,$encrypted,$this->pi_key);
$encrypted = $this->urlsafe_b64encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
return $encrypted;
}*/
public function PrivateEncrypt($data){
// openssl_private_encrypt($data,$encrypted,$this->pi_key);
$crypto = '';
foreach (str_split($data, 117) as $chunk) {
openssl_private_encrypt($chunk, $encryptData, $this->pi_key);
$crypto .= $encryptData;
}
$encrypted = $this->urlsafe_b64encode($crypto);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
return $encrypted;
}
//加密码时把特殊符号替换成URL可以带的内容
function urlsafe_b64encode($string) {
$data = base64_encode($string);
$data = str_replace(array('+','/','='),array('-','_',''),$data);
return $data;
}
//解密码时把转换后的符号替换特殊符号
function urlsafe_b64decode($string) {
$data = str_replace(array('-','_'),array('+','/'),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
//私钥加密的内容通过公钥可用解密出来
public function PublicDecrypt($encrypted){
// $encrypted = $this->urlsafe_b64decode($encrypted);
$crypto = '';
foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) {
openssl_public_decrypt($chunk, $decryptData, $this->pu_key);
$crypto .= $decryptData;
}
//openssl_public_decrypt($encrypted,$decrypted,$this->pu_key);//私钥加密的内容通过公钥可用解密出来
return $crypto;
}
//公钥加密
public function PublicEncrypt($data){
//openssl_public_encrypt($data,$encrypted,$this->pu_key);//公钥加密
$crypto = '';
foreach (str_split($data, 117) as $chunk) {
openssl_public_encrypt($chunk, $encryptData, $this->pu_key);
$crypto .= $encryptData;
}
$encrypted = $this->urlsafe_b64encode($crypto);
return $encrypted;
}
//私钥解密
public function PrivateDecrypt($encrypted)
{
$crypto = '';
foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) {
openssl_private_decrypt($chunk, $decryptData, $this->pi_key);
$crypto .= $decryptData;
}
//$encrypted = $this->urlsafe_b64decode($encrypted);
//openssl_private_decrypt($encrypted,$decrypted,$this->pi_key);
return $crypto;
}
}
不用我多写什么了吧,有问题可以直接联系我。
来源:http://blog.csdn.net/httpp886/article/details/54694903
标签:rsa,密文,加密,解密
0
投稿
猜你喜欢
python的变量与赋值详细分析
2022-06-14 23:09:44
使用python matplotlib 画图导入到word中如何保证分辨率
2023-06-30 21:07:31
SQL2005学习笔记 APPLY 运算符
2024-01-27 16:39:22
SQL中Groupby和Having的使用方法
2008-12-29 13:54:00
Session的工作机制详解和安全性问题(PHP实例讲解)
2024-05-03 15:29:39
python计算两个地址之间的距离方法
2023-09-01 01:40:59
ASP小偷(远程数据获取)程序的入门教程
2007-09-21 12:48:00
用户研究角度看设计(1)“复制链接”的故事
2008-12-26 17:48:00
JavaScript与JQuery框架基础入门教程
2024-04-22 22:23:08
浅谈Python对内存的使用(深浅拷贝)
2021-06-08 03:07:35
python爬虫利用selenium实现自动翻页爬取某鱼数据的思路详解
2021-05-20 08:43:26
Python数据分析之双色球统计两个红和蓝球哪组合比例高的方法
2021-05-14 01:15:57
连接mysql的常用工具分享
2024-01-21 20:01:18
Python实现将Excel转换成xml的方法示例
2023-01-19 23:02:53
ACCESS中Field对象的标题属性
2008-11-20 17:44:00
python使用SMTP发送qq或sina邮件
2021-03-08 01:06:55
用Python将IP地址在整型和字符串之间轻松转换
2021-03-31 16:37:16
python数据可视化Seaborn绘制山脊图
2023-12-22 09:04:17
python requests库爬取豆瓣电视剧数据并保存到本地详解
2022-05-29 08:35:05
25个值得收藏的Python文本处理案例
2022-06-19 15:38:29