PHP - php_function_openssl_pkey_get_private()
Definition and Usage
The openssl_pkey_get_private() function will return you the private key.
Description
The function openssl_pkey_get_private() returns private key from the given public/private key. For example contents from .pem file.
Syntax
openssl_pkey_get_private ( mixed $key [, string $passphrase = "" ] ) : resource
Parameters
| Sr.No | Parameter | Description |
|---|---|---|
| 1 |
key |
You can take the key from the .pem file or using private key generated from openssl_pkey_new(). |
| 2 |
passphrase |
If they key you are using is encrypted , than you will have to specify passphrase. |
Return Values
PHP openssl_pkey_get_private() function returns a resource identifier if there is no error. It will return false if the key generation fails.
PHP Version
This function will work from PHP Version greater than 5.0.0.
Example 1
Working of openssl_pkey_get_private():
<?php
// Generate a new private (and public) key pair
$privkey = openssl_pkey_new();
openssl_pkey_export($privkey, $yourprivatekey);
$testprivatekey = openssl_pkey_get_private($yourprivatekey);
if ($testprivatekey === false) {
var_dump(openssl_error_string());
} else {
var_dump($testprivatekey);
}
?>
This will produce the following result:
resource(3) of type (OpenSSL key)
Example 2
Working of openssl_pkey_get_private() and openssl_pkey_get_details()
<?php
$privkey = openssl_pkey_new();
openssl_pkey_export($privkey, $yourprivatekey);
$testprivatekey = openssl_pkey_get_private($yourprivatekey);
if ($testprivatekey === false) {
var_dump(openssl_error_string());
} else {
//var_dump($testprivatekey);
$key_details = openssl_pkey_get_details($testprivatekey);
print_r($key_details);
}
?>
This will produce the following result:
Array
(
[bits] => 2048
[key] => -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnO1F0Gt03mgxLcDpRxlm
VEh8sfIfan5o11EcoLaEwaTyKgMNm7A4Rs9LcG3fwkNjXo8XNRQv6OSrhl8y00AX
+cPUb78Qp/K3INmyXr8UUVWy0BT+Rxq5kez1TmZhpITxUYLScEL8DPcghFyfstRa
5hP8hc0cwYM6N4ieOlXIxrdmbzTn92MeyiTstTvHxt8aGKbSdpmksWyNeqke22kM
9pBfEsf98XFh5HoQBQaQF6GXL5y00PWIdki7DTzYtXXPRGcQl/53M5HrGGdP0kGS
uD4YOFXRWYtQR1ZYLa4Ej+BP0eOpfxXiME0aaH1/2iWqyi+bsvkdgFbu92j5ptsr
yQIDAQAB
-----END PUBLIC KEY-----
[rsa] => Array
(
[n] => Ekth1-GfTH|j~hQ*
8FKpmCc^5/_2@o ^QUGNfaQpB \Z:7:Ufo4c$;vlzi_qaz/vH
8UYPGVX-O0Mh}%/Vh+
[e] =>
[d] => MKC|Y55}ZR0;SIVM0LwrR|CdW}#v[9iZI1&!A;8K%}`@
}n b]KL#~Sg' 81!2R]`vl&Z@-q7u#.d]*Z-'| ?M
j
[p] => *&5IXMU$u'd,$xiR1ACxi`%FR5
!aCt^ f$V+]v"+=IwRN#YN},pk}g
[dmp1] =>
l5o5Lh)I+DnJ!ycP1
=u>&h-HTKj;um
oO\e.
X%s_|Za3z$\n1N1uj)z
[dmq1] => n[i q|$GI'R^c
"e"k
[iqmp] => ij a6W#:#bS?Sr#C87])4Z`w.ay@ psXP.{o9hBqEUn &StE
)
[type] => 0
)
Example 3
Working of openssl_pkey_get_private() with passphrase:
<?php
$privkey = openssl_pkey_new();
openssl_pkey_export($privkey, $testkey, 'helloworld');
$testprivatekey = openssl_pkey_get_private($testkey, 'helloworld');
if ($testprivatekey === false) {
var_dump(openssl_error_string());
} else {
//var_dump($testprivatekey);
$key_details = openssl_pkey_get_details($testprivatekey);
print_r($key_details);
}
?>
This will produce the following result:
Array
(
[bits] => 2048
[key] => -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs4HV+NM9dQ1ssuxb9PhM
64Yn8RHgv7YKK33nZudmk6SCOr9yRo7immp+bkaA0Lt/9ONJP+UF5VCltpdNdHLb
GDTo+TkK5NdTJDalON3L9EhB4cJeQaQQh59uJGf39Et0BJiYiINXsNdlc/pig1/Z
XDRyhEtqQ6nZJkOIIWO0gpdj9xj2naq/wy6Oas4p3/A7EvN5nB22xfEVIptKUZzi
YWV4Bs5y2OM3GRwVv+jLYKQ49S/ZKq7MpdCxcXC6YxyrlEIz4PL9cWRtybK3BINv
JnCWrfWRhUtlAY/CvfXrq2PhXwHVcebsDOPob6A71TcZMirxFZVyqkC+4rGX+5be
PwIDAQAB
-----END PUBLIC KEY-----
[rsa] => Array
(
[n] => =u
l[L'
+}ff:rFj~nFI?PMtr49
S$68HA^An$gKtWesb_\4rKjC&C!cc.j);y"JQaexr7`8/*qpcB3qdmo&pKec_qo;72*r@?
[e] =>
[d] => ee$%
sQEUADBu34$v#P\uD(zw+za5XqP%O+3]+o3FM*Z^Lk/Ty/;)sBNp5@x,8z2#SyMoX.;K qsLLD
^"j*=Io{7`a{lB#\
[p] => tY@[Nnpf ?7/\yiwXP5@s BDC| *JH/|`+3]oc?fIZe`C>U|":U^Lg u|@zF%6X/
+_Yh:59}D&Gc
[dmp1] => i*/
U6
|>3HPhN.G9cxj*!0R:9E(Z$&&7>9.IhEz/,QR"';^xq
[dmq1] => |w`
3bAZt.8Q]FiI2){ \p5N
$WCj |{>dY
f93$rF(ON> %`(
&op }=C+b#G"0T.G`t^\7
)
[type] => 0
)
Example 4
Working of openssl_pkey_get_private() with .pem file:
<?php
//creating private key
$privkey = openssl_pkey_new();
openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/fortesting.pem');
//using .pem file with private key.
$testprivatekey = openssl_pkey_get_private(file_get_contents('C:/xampp/htdocs/modules/openssl/fortesting.pem'));
if ($testprivatekey === false) {
var_dump(openssl_error_string());
} else {
//var_dump($testprivatekey);
$key_details = openssl_pkey_get_details($testprivatekey);
print_r($key_details);
}
?>
This will produce the following result:
Array
(
[bits] => 2048
[key] => -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqaka1+sKh3D4HgIDzER6
vr/DI5u6h1QF6Xm1q/nOduTn0vlx4bLv+QEbcElXV1Bss4W7wOZRkMIOwj4xcT+e
PGKaN95JUyxC/NQ13+F6K5yUk0ish36BVusrHt0wdZj28f63gHm824D0yDnn+aJr
s+vSuMppErUD/i0QUFnO86ypHi/zeb+QBEif4a82RtfwRIVUtE/Sxy08ct+1ogA9
pdBd47elLmcekz/dtSUqpUjLj5SNojS7AJCZ5LNxnLOzN3ryCQXGaAn8KHQ284Xs
jlYBjSjXFLY/1fLDYDpQGOApoBj2vK9Io8MxFU3uss79Ezb6LwKZG6CmzlbldBrJ
YQIDAQAB
-----END PUBLIC KEY-----
[rsa] => Array
(
[n] =>
pDz#TyvqpIWWPlQ>1q?
[d] => 9:Y"*xu> &"{
!Pxrhn#!c%u
x7z&|vXT?[w"=em1R_JH/hX.E&&':/:.Izdx@6)i1Lz"X>]tRhg!8Y9Gr9zz~jo_
[p] => +iym:#`BFBj>f2W%eIAn .o v`tggaL%
L[\6`sx]~bUfF/Oy6+~v7u
[q] => u(.LloGcx@4r `iiXv'33Xc_m8M5#t"^{j vSLiXmhI8
1t
[dmp1] => 6$5EgQ1F"8yV`\A{0
ym6dUI il5w*eG~z2{*"`yr%@lqDeUz
s.[
Gl_h8H4N7$25?L
[iqmp] => 9"l@O[I)}K6A/f5S \u>C z]B-ry/DwH|g:8s(BEo` 0
)