PHP - openssl_private_decrypt() 函数



定义和用法

openssl_private_decrypt() 函数将使用私钥解密数据。

描述

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

语法

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

参数

序号 参数 描述
1

data

要解密的数据。

2

decrypted

它将包含已解密的数据。

3

key

私钥。

4

padding

您可以应用的填充包括:OPENSSL_PKCS1_PADDING、OPENSSL_SSLV23_PADDING、OPENSSL_PKCS1_OAEP_PADDING、OPENSSL_NO_PADDING。

返回值

PHP openssl_private_encrypt() 函数在成功时返回 TRUE,失败时返回 FALSE。

PHP 版本

此函数适用于 PHP 5.0.0 及更高版本。

示例 1

使用 openssl_public_encrypt() 加密数据并使用 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" => "[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');
	
	
   // 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;
   }
?>

这将产生以下结果

Data encryption : L�_}{�E*?��9[w��7p �\ϸI�?ݟ'��ݹ�n��!��ɿ�*��Xcw���Ւ�)��/��{��!j�L��I*Ï"9eV�9�=Y\�m�i䁦�M(�0PJ��Ԇ�9��C�`�a�ݧ�b��a��?�m�G$i��eU/[�eU����\=�zLdŌn"��:[\�UA��ԭ�ힲ2@-"d��s�=2�nˣ�h��q5U��浿��9�{ݼ��|�NE�a!

Data decryption : Welcome To TuorialsPoint

示例 2

使用 openssl_public_encrypt() 加密文件内容并使用 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" => "[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');
	
	
   // To encrpt data
   $data = file_get_contents('C:/xampp/htdocs/modules/openssl/test.txt'); // save any data in file test.txt;
   $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;
   }
?>

这将产生以下结果

Data encryption : �ى1�A��c�`�xi�j&�!$�z˚�i�*:�)�W��ai�ٶ����|t��u��m3S��`��B�fF�qu �r�Z����2X����l�P��7�{ ��v�3(5�RdCř6�]�X`�[�2��{��d�̣bP���YHz �*ب��ZX�R�RWG*��.��˸>�|f�#ԇ]�K����@ж�'�hp��À�7�T���mΓ�&��BeR���cu�Y��ƣ��-� 

Data decryption : This is data encryption test using openssl php module.
php_function_reference.htm
广告