PHP - Function openssl_public_encrypt()
Definition and Usage
The openssl_public_encrypt() function will encrypt the data with public key.
Description
Using function openssl_public_encrypt() the data will be encrypted and it can be decrypted using openssl_private_decrypt().
Syntax
openssl_public_encrypt ( string $data , string &$crypted , mixed $key [, int $padding = OPENSSL_PKCS1_PADDING ] ) : bool
Parameters
| Sr.No | Parameter | Description |
|---|---|---|
| 1 |
data |
. |
| 2 |
encrypted |
It will have the data that is encrypted. |
| 3 |
key |
The public key. |
| 4 |
padding |
The padding you can apply are : OPENSSL_PKCS1_PADDING, OPENSSL_SSLV23_PADDING, OPENSSL_PKCS1_OAEP_PADDING, OPENSSL_NO_PADDING. |
Return Values
PHP openssl_public_encrypt() function returns TRUE on success or FALSE on failure.
PHP Version
This function will work from PHP Version greater than 5.0.0.
Example 1
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 : E wC+cf*oW7EW$p.rng_NA12U~sap)w= #g;u_%Zbb&mv&qk
Example 2
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[w7p \I?'n!*Xcw)/{!jLI*"9eV9=Y\miM(0PJ9C`aba?mG$ieU/[eU\=zLdn":[\UA2@-"ds=2nhq5U9{|NEa!
Data decryption : Welcome To TuorialsPoint