PHP - openssl_public_decrypt() 函数



定义和用法

openssl_public_decrypt() 函数使用公钥解密数据。

描述

openssl_public_decrypt() 函数解密使用 openssl_private_encrypt() 加密的数据。

语法

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

参数

序号 参数 描述
1

data

使用 openssl_private_encrypt() 加密的数据。

2

decrypted

解密后的数据。

3

key

公钥。

4

padding

可用的填充方式:OPENSSL_PKCS1_PADDING, OPENSSL_NO_PADDING。

返回值

PHP openssl_public_decrypt() 函数成功返回 TRUE,失败返回 FALSE。

PHP 版本

该函数适用于 PHP 5.0.0 以上版本。

示例 1

使用 openssl_private_encrypt() 加密数据,并使用 openssl_public_decrypt() 解密。

<?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 TuorialsPoint';
   $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" => "test1",
      "organizationName" => "test2",
      "organizationalUnitName" => "test3",
      "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;
   }
?>

这将产生以下结果

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 TuorialsPoint

示例 2

使用 openssl_private_encrypt() 加密文件内容,并使用 openssl_public_decrypt() 解密。

<?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 = file_get_contents('C:/xampp/htdocs/modules/openssl/test.txt');
   $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" => "test1",
      "organizationName" => "test2",
      "organizationalUnitName" => "test3",
      "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;
   }
?>

这将产生以下结果

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 data encryption test using openssl php module.
php_function_reference.htm
广告