PHP - Function openssl_public_encrypt()

The openssl_public_encrypt() function will encrypt the data with public key.
Using function openssl_public_encrypt() the data will be encrypted and it can be decrypted using openssl_private_decrypt().
openssl_public_encrypt ( string $data , string &$crypted , mixed $key [, int $padding = OPENSSL_PKCS1_PADDING ] ) : bool

data
.
encrypted
It will have the data that is encrypted.
key
The public key.
padding
The padding you can apply are : OPENSSL_PKCS1_PADDING, OPENSSL_SSLV23_PADDING, OPENSSL_PKCS1_OAEP_PADDING, OPENSSL_NO_PADDING.
PHP openssl_public_encrypt() function returns TRUE on success or FALSE on failure.
This function will work from PHP Version greater than 5.0.0.
Using openssl_public_encrypt() to Encrpt Data using Public Key:
<?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" => "IN",
      "stateOrProvinceName" => "Karnataka",
      "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 TuorialsPoint';
   $isvalid = openssl_public_encrypt ($data, $crypted , file_get_contents('C:/xampp/htdocs/modules/openssl/publickey.pem'),OPENSSL_PKCS1_PADDING);	
   echo "Data encryption : ".$crypted;
?>
This will produce the following result:
Data encryption : ��u001cE �wC�ݭf�+c��f*��o�u001b��u000eW�7�Eu0005W��$�p�.rng�_N�u0016�A1u001d��2Uݴu0002~su0015�ap۳)w��=� ��#��g;�u0001��u001eu��_%�Z�bb�u0002&��mu0002�u0005�v&��u0010��q��k

To encrypt data using openssl_public_encrypt() and decrypt using  openssl_private_decrypt():
<?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" => "IN",
      "stateOrProvinceName" => "Karnataka",
      "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 TuorialsPoint';
   $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;
   }
?>
This will produce the following result:
Data encryption : L�_}{�E*?��9[w��7p �u001d\ϸI�?ݟ'��ݹ�n��!��ɿu001f�*��Xcw�u0005��u001eՒ�)��/��{�u0011�!j�L��I*Ï"9eV�u0018u00149�=u001aY\�m�i䁦�M(�0PJ��Ԇ�9�u000e�C�`�au001e�ݧ�bu0014��au0014��?�m�G$i��eUu001f/[u001f�feU��u0017��\=�zLdŌn"�u0005�u001fu0013:[\u0013u001fu0005�Uu0014A��ԭ�ힲ2u0011@f-u0007"u0004du001c��s�=2�nx�hu0006��q5U��浿��9�{ݼ�u000b�|�NE�u000ea!

Data decryption : Welcome To TuorialsPoint


    45 Lectures 
     9 hours 

 Malhar Lathkar

    34 Lectures 
     4 hours 

 Syed Raza

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    17 Lectures 
     1 hours 

 Nivedita Jain

    100 Lectures 
     34 hours 

 Azaz Patel

    43 Lectures 
     5.5 hours 

 Vijay Kumar Parvatha Reddy
 Print
 Add Notes
 Bookmark this page