PHP Asymmetric Encryption Based on openssl

  • 2021-11-13 01:09:18
  • OfStack

This paper describes the asymmetric encryption operation of PHP based on openssl. Share it for your reference, as follows:

Asymmetric encryption is mainly used with the help of openssl public key and private key, using public key encryption and private key decryption, or private key encryption and public key decryption.

1. Install openssl extensions for openssl and php

2. Generate private key: openssl genrsa is used to generate rsa private key file. The length of private key and password protection can be specified for generation


openssl genrsa -out rsa_private_key.pem 1024

3. Generate the public key: The rsa command handles the RSA key, format conversion, and print information


openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem

4. Here we use private key encryption and public key decryption


<?php
/**
*  Path to the key file 
*/
$privateKeyFilePath = 'rsa_private_key.pem';
/**
*  Path to the public key file 
*/
$publicKeyFilePath = 'rsa_public_key.pem';
extension_loaded('openssl') or die('php Need openssl Extended support ');
(file_exists($privateKeyFilePath) && file_exists($publicKeyFilePath)) or die(' The file path of the key or public key is incorrect ');
/**
*  Generate Resource If the contents of the key file are corrupted, openssl_pkey_get_private Function returns false
*/
$privateKey = openssl_pkey_get_private(file_get_contents($privateKeyFilePath));
/**
*  Generate Resource If the contents of the public key file are corrupted, openssl_pkey_get_public Function returns false
*/
$publicKey = openssl_pkey_get_public(file_get_contents($publicKeyFilePath));
($privateKey && $publicKey) or die(' The key or public key is not available ');
/**
*  Original data 
*/
$originalData = ' Before encryption hahahaha';
/**
*  Encrypted data for transmission over the network 
*/
$encryptData = '';
echo ' The original data is :', $originalData, PHP_EOL;
/////////////////////////////// Encryption with private key ////////////////////////
if (openssl_private_encrypt($originalData, $encryptData, $privateKey)) {
  /**
   *  After encryption   Yes base64_encode After that, it is convenient to transmit in the website address   Or print   Otherwise, it will be printed as garbled 
   */
  echo ' Encrypted successfully, encrypted data (base64_encode Posterior ) For :', base64_encode($encryptData), PHP_EOL;
} else {
  die(' Encryption failed ');
}
/////////////////////////////// Decrypt with public key ////////////////////////
/**
*  Data after decryption 
*/
$decryptData ='';
if (openssl_public_decrypt($encryptData, $decryptData, $publicKey)) {
  echo ' Decryption is successful, and the decrypted data is :', $decryptData, PHP_EOL;
} else {
  die(' Decryption succeeded ');
}

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

Online DES Encryption/Decryption Tool
http://tools.ofstack.com/password/des_encode

MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
http://tools.ofstack.com/password/sha_encode

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of php Encryption Methods", "Summary of PHP Encoding and Transcoding Operation Skills", "Summary of PHP Mathematical Operation Skills", "Complete Collection of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Tutorial on PHP Data Structure and Algorithm", "Summary of php Programming Algorithm" and "Summary of php Regular Expression Usage"

I hope this article is helpful to everyone's PHP programming.


Related articles: