openssl_private_decrypt()函數(shù)用于使用私鑰解密數(shù)據(jù)。
openssl_private_decrypt()函數(shù)將使用私鑰解密數(shù)據(jù)。
使用函數(shù)openssl_private_decrypt()將解密使用openssl_private_encrypt()加密的數(shù)據(jù)。
openssl_private_decrypt ( string $data , string &$decrypted , mixed $key [, int $padding = OPENSSL_PKCS1_PADDING ] ) : bool
序號(hào) | 參數(shù) | 描述 |
---|---|---|
1 | data | 要解密的數(shù)據(jù)。 |
2 | decrypted | 它將具有解密的數(shù)據(jù)。 |
3 | key | 私鑰。 |
4 | padding | 您可以應(yīng)用的填充為:OPENSSL_PKCS1_PADDING,OPENSSL_SSLV23_PADDING,OPENSSL_PKCS1_OAEP_PADDING,OPENSSL_NO_PADDING .. |
PHP openssl_private_encrypt()函數(shù)成功時(shí)返回TRUE,失敗時(shí)返回FALSE。
此函數(shù)將從大于5.0.0的PHP版本開始工作。
要使用openssl_public_encrypt()加密數(shù)據(jù)并使用openssl_private_decrypt()解密:
<?php //保存私鑰 $privkey = openssl_pkey_new(); openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/privatekey.pem'); //保存公鑰 $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'); //加密數(shù)據(jù) $data = 'Welcome To (cainiaoplus.com)'; $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; } ?>
這將產(chǎn)生以下結(jié)果:
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 (cainiaoplus.com)
要使用openssl_public_encrypt()加密文件內(nèi)容并使用openssl_private_decrypt()解密:
<?php //保存私鑰 $privkey = openssl_pkey_new(); openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/privatekey.pem'); //保存公鑰 $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'); // 加密數(shù)據(jù) $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; } ?>
這將產(chǎn)生以下結(jié)果:
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.