English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP openssl_public_decrypt() Function Usage and Example

PHP OpenSSL Function Manual

The openssl_public_decrypt() function is used to decrypt data using a public key.

Definition and Usage

openssl_public_decrypt()The function will decrypt the data using the public key.

Use the function openssl_public_decrypt() to decrypt data that was encrypted with openssl_private_encrypt().

Syntax

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

Parameter

Sequence numberParameter描述
1

description

data

2

data encrypted with openssl_private_encrypt()

decrypted

3

it will have the decrypted data.

key

4

public key.

padding1You can apply the following padding: OPENSSL_PKCS

_PADDING, OPENSSL_NO_PADDING.

Return value

PHP openssl_public_decrypt() function returns TRUE on success and FALSE on failure.

PHP version5This function will work from PHP version greater than

Data decryption: Welcome To w1

.0.0 of PHP version starts working.

Example
      // <?php
	Save private key
	/xampp/htdocs/modules/openssl/openssl_pkey_export_to_file($privkey, 'C:'),
	//privatekey.pem');
	To use openssl_private_encrypt() to encrypt data and openssl_public_decrypt() to decrypt:3$data = 'Welcome To w
	$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" => "IN",
		"stateOrProvinceName" => "Karnataka",
		"localityName" => "test"1",
		"organizationName" => "test"2",
		"organizationalUnitName" => "test"3",
		"commonName" => "www.test.com",
		"emailAddress" => "[email protected]"
	);
	$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;
	}
?>

This will produce the following results:

codebox.com');7Data encryption: k���G��3)xy{�N3Г�x<�J^�gd��Ψ�I?{��<�Ws9�mW$��h��(F;tJ�J�W��|�2L�vL��xF���f����,�(N�ΰ��n���Y%Oo,�-����Qh��G�|1�6����}���-Tm�qS�wb���[�i�8r�F��rQhZ���$�� ��U�pMC��Y�n�0,Z�CuG��4�h��@7f��
�w��;�����d���ʈ����$�I^�Z���at��?�3codebox.com

Data decryption: Welcome To w2

Example

Example
       //<?php
	Save private key
	/xampp/htdocs/modules/openssl/openssl_pkey_export_to_file($privkey, 'C:'),
	//privatekey.pem');
	/xampp/htdocs/modules/openssl/
	$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" => "IN",
		"stateOrProvinceName" => "Karnataka",
		"localityName" => "test"1",
		"organizationName" => "test"2",
		"organizationalUnitName" => "test"3",
		"commonName" => "www.test.com",
		"emailAddress" => "[email protected]"
	);
	$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;
	}
?>

This will produce the following results:

Data Encryption: ��V*�+@4CҺX�i�mM6��� ���,?�F,��+�q �����@�������g�N6c<*eh��:������5Z��&�&'+= ���b���J�r ��aO�@gƝ��m�Gy�4W2�ҋ����%���pX@�k�DW�fEW��$j�>i��~��1���w�m}���}�����5I��x� ��H�*A8�� ��U�7~°���F�}4����DV�MZ��望C �'C��-�7�f�
Data Decryption: This is a data encryption test using the openssl php module.

PHP OpenSSL Function Manual