2

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

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.

2
  • What format is that hex dump? Commented May 21, 2012 at 1:32
  • To be honest, I am not sure about the name of the format. But I got this by viewing certificate details from the browser. Commented May 21, 2012 at 1:36

1 Answer 1

5

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"]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.