PHP Implementation of RSA Encryption and Decryption Algorithm Example of Generation of Key Bits for 1024 Bits

  • 2021-09-12 00:41:38
  • OfStack

You can first go to the website http://web.chacuo.net/netrsakeypair to generate public and private keys online

RSA asymmetric encryption algorithm, if it is public key encryption, you have to use private key decryption, in turn also 1, private key encryption on the use of public key decryption, the following is related to the implementation of the function


/**
 * RSA Private key encryption 
 * @param string $private_key  Private key 
 * @param string $data  String to encrypt 
 * @return string $encrypted  Returns an encrypted string 
 * @author mosishu
 */
function privateEncrypt($private_key,$data){
 $encrypted = '';
 $pi_key = openssl_pkey_get_private($private_key);// This function can be used to determine whether the private key is available and can return resources id Resource id
 // The maximum allowed encryption length is 117 Sectional encryption 
 $plainData = str_split($data, 100);// Number of bits of generated key  1024 bit key
 foreach($plainData as $chunk){
  $partialEncrypted = '';
  $encryptionOk = openssl_private_encrypt($chunk,$partialEncrypted,$pi_key);// Private key encryption 
  if($encryptionOk === false){
   return false;
  }
  $encrypted .= $partialEncrypted;
 }
 
 $encrypted = base64_encode($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;
}

/**
 * RSA Public key decryption ( The contents encrypted by the private key can be decrypted by the public key )
 * @param string $public_key  Public key 
 * @param string $data  String encrypted by private key 
 * @return string $decrypted  Returns the decrypted string 
 * @author mosishu
 */
function publicDecrypt($public_key,$data){
 $decrypted = '';
 $pu_key = openssl_pkey_get_public($public_key);// This function can be used to determine whether the public key is available 
 $plainData = str_split(base64_decode($data), 128);// Number of bits of generated key  1024 bit key
 foreach($plainData as $chunk){
  $str = '';
  $decryptionOk = openssl_public_decrypt($chunk,$str,$pu_key);// Public key decryption 
  if($decryptionOk === false){
   return false;
  }
  $decrypted .= $str;
 }
 return $decrypted;
}

//RSA Public key encryption 
function publicEncrypt($public_key,$data){
 $encrypted = '';
 $pu_key = openssl_pkey_get_public($public_key);
 $plainData = str_split($data, 100);
 foreach($plainData as $chunk){
  $partialEncrypted = '';
  $encryptionOk = openssl_public_encrypt($chunk,$partialEncrypted,$pu_key);// Public key encryption 
  if($encryptionOk === false){
   return false;
  }
  $encrypted .= $partialEncrypted;
 }
 $encrypted = base64_encode($encrypted);
 return $encrypted;
}

//RSA Private key decryption 
function privateDecrypt($private_key,$data){
 $decrypted = '';
 $pi_key = openssl_pkey_get_private($private_key);
 $plainData = str_split(base64_decode($data), 128); 
 foreach($plainData as $chunk){
  $str = '';
  $decryptionOk = openssl_private_decrypt($chunk,$str,$pi_key);// Private key decryption 
  if($decryptionOk === false){
   return false;
  }
  $decrypted .= $str;
 }
 return $decrypted;
}

Related articles: