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_encrypt() Function.
The openssl_private_encrypt() function is used to encrypt the data with private key.
This built-in function encrypts the data and it can be decrypted using the built-in PHP openssl_public_decrypt() function.
The openssl_private_encrypt() function is used to encrypt the data with private key.
This built-in function encrypts the data and it can be decrypted using the built-in PHP openssl_public_decrypt() function.
syntax
Following below is the syntax to use this function -
openssl_private_encrypt ( string $data , string &$crypted , mixed $key [, int $padding = OPENSSL_PKCS1_PADDING ] ) : bool
Parameter Details
Sr.No | Parameter | Description |
---|---|---|
1 | data | |
2 | encrypted | It will have the data that is encrypted. |
3 | key | The private key. |
4 | padding | The padding you can apply are : OPENSSL_PKCS1_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 illustrates the usage of PHP openssl_private_encrypt() function to encrypt data using Private Key -
<?php // To encrpt data $privkey = openssl_pkey_new(); openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/privatekey.pem'); $data = 'Welcome To WebDesignTutorialz'; openssl_private_encrypt ($data, $crypted , file_get_contents('C:/xampp/htdocs/modules/openssl/privatekey.pem'),OPENSSL_PKCS1_PADDING); echo $crypted; ?>
Output
When the above code is executed, it will produce the following result -
��Z甌�3�g[.zT�J�tn��g�M�P>���7U��k�vJ�@/��ɥ���U�j��ː�RC��bQGQ: �NN����Z��#J0J ��C�t�SC���pxol� ����U��'�V7[�#��{�]�Fa��#�p_��Y��<�xYKƛR> kRs�t��:��.Is*5YP-�]�^��2(t0��!Td}�],HeUP�p��ާև� 9e;��\��h��R
Example2
The following example below illustrates the usage of the openssl_private_encrypt() and the openssl_public_decrypt() functions for encrypting and decrypting data -
<?php // Save Private Key $privkey = openssl_pkey_new(); openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/privatekey.pem'); // To encrpt data $data = 'Welcome To WebDesignTutorialz'; $isvalid = openssl_private_encrypt ($data, $crypted , file_get_contents('C:/xampp/htdocs/modules/openssl/privatekey.pem'),OPENSSL_PKCS1_PADDING); echo "Data encryption : ".$crypted; echo ">br/<>br/<"; //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'); if ($isvalid) { openssl_public_decrypt ($crypted, $decrypted , file_get_contents('C:/xampp/htdocs/modules/openssl/publickey.pem'),OPENSSL_PKCS1_PADDING); echo "Data decryption : ".$decrypted; } ?>
Output
When the above code is executed, it will produce the following result -
Data encryption : k��G��7)xy{�N3Г�x<�J^�gd��Ψ�I?{��<�Ws3�mW$��h��(F;tJ�J�W��|�9L�vL��xF��f��,�(N�ΰ��n���Y%Oo,�2��Qh��G�|-��}���1�6Tm�qS�wb��[�i�-r�F��rQhZ��$�� ��U�pMC��Y�n�0,Z�CuG��8�h��@4f��7ؕ�w��;��d��ʈ��$�I^�Z��at��?� Data decryption : Welcome To WebDesignTutorialz
Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we will be studying about the openssl_public_encrypt() Function.
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.
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.