简体   繁体   中英

How to extract public key in proper format using openssl in php?

I have made many attempts using differnt ways, to extract public key in displayable format using openssl in php. Eg

print_r(openssl_pkey_get_details(openssl_csr_get_public_key(\\path to csr)));

var_dump(openssl_get_publickey(file_get_contents('\\path to cert'))); 

and many more. But all I can get is something like this:

-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQClNami19DpcxlYAaZNxHV27r2V gDQbkZhiayaDfcwjYStRaU8Hk1yg76gfhgayssIa6Y7Cek4uH5fV PE6Nj/s9QAkcvpzZDJasdYmj8BGyVwVbRelToMNvXTc eNaH93Dm+OA4TE9yoQIDAQAB

-----END PUBLIC KEY-----

How can I extract it in the format given below:

95 ae 9a 4e db f1 6d 15 55 9f 86 52 28 54 21 3f 88 1b 21 81 2a 01 e3 35 dd 21 51 44 f4 18 bf 85 fb f0 6a 9a 9c 15 7f 46 83 b8 1e 05 b7 b9 1a 9d fd 58 0b fa 45 01 f2 3b 3b bc 1b f6 a3 20 7b 96 3e f7 5d d6 c2 a7 56 29 02 94 ba 0c 29 da 51

Thank you.

The public key listed by a browser is just a hexadecimal encoding of the modulus. Within the data structure returned by openssl_pkey_get_details this is represented by n . (If you're curious why, check out the RSA algorithm.) To get it in PHP just do:

$pkey_details = openssl_pkey_get_details(openssl_pkey_get_public(file_get_contents("test.pem")));
echo bin2hex($pkey_details["rsa"]["n"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM