3

There are Private Key, Public Key, x509 Pem file. I want to acquire information about RSA Public Key from these files.

I can confirm it by the shell script as follows. $ openssl x509 -in cert.pem -text

...

    Subject Public Key Info:
        Public Key Algorithm: rsaEncryption
        RSA Public Key: (2048 bit)
            Modulus (2048 bit):
                00:e1:92:dc:05:84:c7:e1:2d:db:f3:48:84:90:32:
                ...
                da:7d:2f:95:d2:ab:28:6e:6c:be:0a:af:e0:cb:24:
                18:db
            Exponent: 65537 (0x10001)

...

Can I acquire a value of Modulus and Exponent in PHP OpenSSL library in the same way?

3 Answers 3

8

You can try the following:

$key = file_get_contents("root/to/private_key.pem");
$data = openssl_pkey_get_private($key);
$data = openssl_pkey_get_details($data);

$key = $data['key'];
$modulus = $data['rsa']['n'];
$exponent = $data['rsa']['e'];

echo "Modulus: $modulus <br>Exponent: $exponent";

In case echoing $exponent shows nothing on screen, you can try var_dump($exponent) or var_dump($data) and you will see some strange chars.

Sign up to request clarification or add additional context in comments.

Comments

1

Using phpseclib, a pure PHP X.509 implementation:

$x509 = new File_X509();
$cert = $x509->loadX509('-----BEGIN CERTIFICATE-----
MIIDITCCAoqgAwIBAgIQT52W2WawmStUwpV8tBV9TTANBgkqhkiG9w0BAQUFADBM
MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0xMTEwMjYwMDAwMDBaFw0x
MzA5MzAyMzU5NTlaMGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
MRYwFAYDVQQHFA1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKFApHb29nbGUgSW5jMRcw
FQYDVQQDFA53d3cuZ29vZ2xlLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC
gYEA3rcmQ6aZhc04pxUJuc8PycNVjIjujI0oJyRLKl6g2Bb6YRhLz21ggNM1QDJy
wI8S2OVOj7my9tkVXlqGMaO6hqpryNlxjMzNJxMenUJdOPanrO/6YvMYgdQkRn8B
d3zGKokUmbuYOR2oGfs5AER9G5RqeC1prcB6LPrQ2iASmNMCAwEAAaOB5zCB5DAM
BgNVHRMBAf8EAjAAMDYGA1UdHwQvMC0wK6ApoCeGJWh0dHA6Ly9jcmwudGhhd3Rl
LmNvbS9UaGF3dGVTR0NDQS5jcmwwKAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUF
BwMCBglghkgBhvhCBAEwcgYIKwYBBQUHAQEEZjBkMCIGCCsGAQUFBzABhhZodHRw
Oi8vb2NzcC50aGF3dGUuY29tMD4GCCsGAQUFBzAChjJodHRwOi8vd3d3LnRoYXd0
ZS5jb20vcmVwb3NpdG9yeS9UaGF3dGVfU0dDX0NBLmNydDANBgkqhkiG9w0BAQUF
AAOBgQAhrNWuyjSJWsKrUtKyNGadeqvu5nzVfsJcKLt0AMkQH0IT/GmKHiSgAgDp
ulvKGQSy068Bsn5fFNum21K5mvMSf3yinDtvmX3qUA12IxL/92ZzKbeVCq3Yi7Le
IOkKcGQRCMha8X2e7GmlpdWC1ycenlbN0nbVeSv3JUMcafC4+Q==
-----END CERTIFICATE-----');

$pubkey = $x509->getPublicKey();
$parts = $pubkey->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_RAW);
echo $parts['e'] . "\r\n\r\n" . $parts['n'];

Comments

0

You probably want openssl_pkey_get_details($key).

This takes a key resource generated from openssl_pkey_get_private() or openssl_pkey_get_public() (?) and will return an array with keys bits, key, type, and a fourth key that is one of rsa, dsa, or dh depending on the key. The fourth key maps to another array with more details; for RSA keys, this subarray contains your modulus n and exponent e.

See this page and related pages in the PHP documentation for more usage notes.

(None of this is tested; let me know if this works for you!)

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.