Solution of over 1024 Encryption and Decryption of Ciphertext Based on PHP RSA

  • 2021-09-12 00:40:57
  • OfStack

As shown below:


<?php
namespace helpers;

class OpensslRSA{

 //echo $private_key  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 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;

 // Determining whether public and private keys are available 
 public function __construct()
 {
  $this->pi_key = openssl_pkey_get_private($this->private_key);// This function can be used to determine whether the private key is available and can return resources id Resource id
  $this->pu_key = openssl_pkey_get_public($this->public_key);// This function can be used to determine whether the public key is available 
  // print_r($this->pi_key);echo "\n";
  // print_r($this->pu_key);echo "\n";
 }

 // Private key encryption 
 /*public function PrivateEncrypt($data){
  openssl_private_encrypt($data,$encrypted,$this->pi_key);
  $encrypted = $this->urlsafe_b64encode($encrypted);// Encrypted content usually contains special characters, which need to be encoded and converted between networks url Pay attention when transmitting base64 Is the code url Safe 
  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);// Encrypted content usually contains special characters, which need to be encoded and converted between networks url Pay attention when transmitting base64 Is the code url Safe 
  return $encrypted;
 }

// When adding passwords, replace special symbols with URL What you can bring 
 function urlsafe_b64encode($string) {
  $data = base64_encode($string);
  $data = str_replace(array('+','/','='),array('-','_',''),$data);
  return $data;
 }

 // Replace special symbols with converted symbols when deciphering passwords 
 function urlsafe_b64decode($string) {
  $data = str_replace(array('-','_'),array('+','/'),$string);
  $mod4 = strlen($data) % 4;
  if ($mod4) {
   $data .= substr('====', $mod4);
  }
  return base64_decode($data);
 }

 // The content encrypted by the private key can be decrypted by the public key 
 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);// The content encrypted by the private key can be decrypted by the public key 
  return $crypto;
 }

 // Public key encryption 
 public function PublicEncrypt($data){
  //openssl_public_encrypt($data,$encrypted,$this->pu_key);// Public key encryption 
  $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;
 }

 // Private key decryption 
 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;
 }
}

Don't need me to write anything more. If you have any questions, you can contact me directly.


Related articles: