We now have a youtube channel. Subscribe!

PHP | openssl_private_decrypt() Function

PHP openssl_private_decrypt() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP openssl_private_decrypt() Function.

The openssl_private_decrypt() function is used to decrypt the data with private key.

This PHP function decrypts the data that was previously encrypted using the PHP openssl_public_encrypt() function.

syntax

Following below is the syntax to use this function -

openssl_private_decrypt ( string $data , string &$decrypted , mixed $key [, int $padding = OPENSSL_PKCS1_PADDING ] ) : bool


Parameter Details

Sr.NoParameterDescription
1

data

The data to be decrypted.

2

decrypted

It will have the data that is decrypted.

3

key

The private key.

4

padding

The padding you can apply are : OPENSSL_PKCS1_PADDING, OPENSSL_SSLV23_PADDING, OPENSSL_PKCS1_OAEP_PADDING, OPENSSL_NO_PADDING..


Return Value

This PHP function returns true on success or false on failure.

PHP Version

This PHP function works from PHP version greater than 5.0.0.

Example1

The following example below illustrates the usage of the openssl_public_encrypt() and the openssl_private_decrypt() functions for the encryption and decryption of data -

<?php
   // Save Private Key
   $privkey = openssl_pkey_new();
   openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/privatekey.pem');
	
   //Save Public Key
   $dn = array(
      "countryName" => "NG",
      "stateOrProvinceName" => "Rivers",
      "localityName" => "test1",
      "organizationName" => "test2",
      "organizationalUnitName" => "test3",
      "commonName" => "www.test.com",
      "emailAddress" => "xyz@test.com"
   );
   $cert = openssl_csr_new($dn, $privkey);
   $cert = openssl_csr_sign($cert, null, $privkey, 365);
   openssl_x509_export_to_file($cert, 'C:/xampp/htdocs/modules/openssl/publickey.pem');
	
	
   // To encrpt data
   $data = 'Welcome To WebDesignTutorialz';
   $isvalid = openssl_public_encrypt ($data, $crypted , file_get_contents('C:/xampp/htdocs/modules/openssl/publickey.pem'),OPENSSL_PKCS1_PADDING);	
   echo "Data encryption : ".$crypted;
   echo ">br/<>br/<";
	
   if ($isvalid) {	
      openssl_private_decrypt ($crypted, $decrypted , file_get_contents('C:/xampp/htdocs/modules/openssl/privatekey.pem'),OPENSSL_PKCS1_PADDING);	
      echo "Data decryption : ".$decrypted;
   }
?>

Output

When the above code is executed, it will produce the following result -

Data encryption : L�_}{�E*?��9[w��7p �\ϸI�?ݟ'��ݹ�n��!��ɿ�*��Xcw���Ւ�)��/��{��!j�L��I*Ï"9eV�9�=Y\�m�i䁦�M(�0PJ��Ԇ�9��C�`�a�ݧ�b��a��?�m�G$i��eU/[�eU����\=�zLdŌn"��:[\�UA��ԭ�ힲ2@-"d��s�=2�nˣ�h��q5U��浿��9�{ݼ��|�NE�a!

Data decryption : Welcome To WebDesignTutorialz

Example2

The following example below illustrates the usage of the openssl_public_encrypt() and the openssl_private_decrypt() functions for encrypting and decrypting file contents -

<?php
   // Save Private Key
   $privkey = openssl_pkey_new();
   openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/privatekey.pem');
	
   //Save Public Key
   $dn = array(
      "countryName" => "NG",
      "stateOrProvinceName" => "Rivers",
      "localityName" => "test1",
      "organizationName" => "test2",
      "organizationalUnitName" => "test3",
      "commonName" => "www.test.com",
      "emailAddress" => "xyz@test.com"
   );
   $cert = openssl_csr_new($dn, $privkey);
   $cert = openssl_csr_sign($cert, null, $privkey, 365);
   openssl_x509_export_to_file($cert, 'C:/xampp/htdocs/modules/openssl/publickey.pem');
	
	
   // To encrpt data
   $data = file_get_contents('C:/xampp/htdocs/modules/openssl/test.txt'); // save any data in file test.txt;
   $isvalid = openssl_public_encrypt ($data, $crypted , file_get_contents('C:/xampp/htdocs/modules/openssl/publickey.pem'),OPENSSL_PKCS1_PADDING);	
   echo "Data encryption : ".$crypted;
   echo ">br/<>br/<";
	
   if ($isvalid) {	
      openssl_private_decrypt ($crypted, $decrypted , file_get_contents('C:/xampp/htdocs/modules/openssl/privatekey.pem'),OPENSSL_PKCS1_PADDING);	
      echo "Data decryption : ".$decrypted;
   }
?>

Output

When the above code is executed, it will produce the following result -

Data encryption : �ى1�A��c�`�xi�j&�!$�z˚�i�*:�)�W��ai�ٶ����|t��u��m3S��`��B�fF�qu �r�Z����2X����l�P��7�{ ��v�3(5�RdCř6�]�X`�[�2��{��d�̣bP���YHz �*ب��ZX�R�RWG*��.��˸>�|f�#ԇ]�K����@ж�'�hp��À�7�T���mΓ�&��BeR���cu�Y��ƣ��-� 

Data decryption : This is data encryption test using openssl php module.


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial post, we will be making a very brief introduction of AJAX to you guys.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain