I am generating private and public keys using OpenSSL in PHP, which I intend to store in a database (although you probably don't need to know PHP to answer this question).
They look like this:
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIi4rlLSKA9/8CAggA
...
-----END ENCRYPTED PRIVATE KEY-----
and
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8YvAFZHSGNITeDNdXFbc
...
-----END PUBLIC KEY-----
(and yes those are just examples)
They have been created like so:
$resource = openssl_pkey_new([
'private_key_bits' => '2048',
"private_key_type" => OPENSSL_KEYTYPE_RSA,
]);
openssl_pkey_export($resource, $privateKey, $passPhrase) === false
$opensslDetails = openssl_pkey_get_details($resource);
$publicKey = $opensslDetails['key'];
I want to know what the maximum length is for these private and public keys.
From my experimentation, I have found that:
- 1704 characters for private keys
- 1834 characters for private keys with passphrase
- 451 characters for public keys
However I haven't found any formal documentation on this to prove that this is the case, so I can't be sure.