定義和用法
這個openssl_public_encrypt()函數將使用公鑰加密數據。
描述
使用函數 openssl_public_encrypt() 數據將被加密,並且可以使用 openssl_private_decrypt() 解密。
用法
openssl_public_encrypt ( string $data , string &$crypted , mixed $key [, int $padding = OPENSSL_PKCS1_PADDING ] ):bool
參數
Sr.No | 參數 | 描述 |
---|---|---|
1 |
data |
。 |
2 |
encrypted |
它將具有加密的數據。 |
3 |
key |
公鑰。 |
4 |
padding |
您可以應用的填充是:OPENSSL_PKCS1_PADDING、OPENSSL_SSLV23_PADDING、OPENSSL_PKCS1_OAEP_PADDING、OPENSSL_NO_PADDING。 |
返回值
PHP openssl_public_encrypt() 函數在成功時返回 TRUE,在失敗時返回 FALSE。
PHP版本
此函數適用於 5.0.0 以上的 PHP 版本。
例子1
使用 openssl_public_encrypt() 使用公鑰加密數據:
<?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" => "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');
// 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;
?>
這將產生以下結果:
Data encryption:��E �wC�ݭ�+c��f*��o���W�7�EW��$�p�.rng�_N��A1��2Uݴ~s�ap۳)w��=� ��#��g;���u��_%�Z�bb�&��m��v&����q��k
例子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" => "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');
// 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
相關用法
- PHP openssl_public_decrypt()用法及代碼示例
- PHP openssl_pkey_export()用法及代碼示例
- PHP openssl_pkcs12_export()用法及代碼示例
- PHP openssl_private_encrypt()用法及代碼示例
- PHP openssl_pkey_new()用法及代碼示例
- PHP openssl_pkcs12_export_to_file()用法及代碼示例
- PHP openssl_pkey_export_to_file()用法及代碼示例
- PHP openssl_pkcs12_read()用法及代碼示例
- PHP openssl_private_decrypt()用法及代碼示例
- PHP openssl_pkey_get_public()用法及代碼示例
- PHP openssl_get_cert_locations()用法及代碼示例
- PHP openssl_cipher_iv_length()用法及代碼示例
- PHP openssl_spki_verify()用法及代碼示例
- PHP openssl_get_curve_names()用法及代碼示例
- PHP openssl_get_cipher_methods()用法及代碼示例
- PHP openssl_spki_export_challenge()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP output_add_rewrite_var()用法及代碼示例
- PHP octdec( )用法及代碼示例
- PHP ord()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP - Function openssl_public_encrypt()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。