欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于PHP RSA密文过长加密解密 越过1024的解决方法

程序员文章站 2022-07-10 11:54:08
如下所示:

如下所示:

<?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;
 }
}

不用我多写什么了吧,有问题可以直接联系我。

以上这篇基于php rsa密文过长加密解密 越过1024的解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。