openssl_private_encrypt()函數(shù)用于使用私鑰加密數(shù)據(jù)。
openssl_private_encrypt()函數(shù)將使用私鑰加密數(shù)據(jù)。
使用openssl_private_encrypt()函數(shù)將對(duì)數(shù)據(jù)進(jìn)行加密,并且可以使用openssl_public_decrypt()對(duì)其進(jìn)行解密。
openssl_private_encrypt ( string $data , string &$crypted , mixed $key [, int $padding = OPENSSL_PKCS1_PADDING ] ) : bool
序號(hào) | 參數(shù) | 描述 |
---|---|---|
1 | data | |
2 | encrypted | 它將擁有加密的數(shù)據(jù)。 |
3 | key | 私鑰。 |
4 | padding | 您可以應(yīng)用的填充為:OPENSSL_PKCS1_PADDING,OPENSSL_NO_PADDING。 |
PHP openssl_private_encrypt()函數(shù)成功時(shí)返回TRUE,失敗時(shí)返回FALSE。
此函數(shù)將從大于5.0.0的PHP版本開始工作。
使用openssl_private_encrypt()使用私鑰加密數(shù)據(jù):
<?php //加密數(shù)據(jù) $privkey = openssl_pkey_new(); openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/privatekey.pem'); $data = 'Welcome To TuorialsPoint'; openssl_private_encrypt ($data, $crypted , file_get_contents('C:/xampp/htdocs/modules/openssl/privatekey.pem'),OPENSSL_PKCS1_PADDING); echo $crypted; ?>
這將產(chǎn)生以下結(jié)果:
????Z甌?3?g[.zT?J?tn??g?M?P>???7U???k?vJ?@/????????U?j?????RC??bQGQ: ?NN????????Z???#J0J ??C?t?SC????pxol? ?????U???'?V7[?#??{?]?Fa??#?p_??Y??<?xYK?R> k?Rs?t???:??.Is*5YP-?]?^??2(t0??!Td}?],HeUP?p????? 9e;?????\???h???R
要使用openssl_private_encrypt()加密數(shù)據(jù)并使用openssl_public_decrypt()解密:
<?php //保存私鑰 $privkey = openssl_pkey_new(); openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/privatekey.pem'); //加密數(shù)據(jù) $data = 'Welcome To (cainiaoplus.com)'; $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/<"; //保存公鑰 $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'); if ($isvalid) { openssl_public_decrypt ($crypted, $decrypted , file_get_contents('C:/xampp/htdocs/modules/openssl/publickey.pem'),OPENSSL_PKCS1_PADDING); echo "Data decryption : ".$decrypted; } ?>
這將產(chǎn)生以下結(jié)果:
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 (cainiaoplus.com)