Export OpenSSL public key to file - php

How can I export only the public key to a file(.pem) with PHP?
Of course it is possible to export the whole certificate and a private key but I can't find a solution to export a public key.
The file should look like
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArDjwWcPjZRQpiMRsG1fG
kudSYEHnSR57dKUoK+qIc++YZSCWG/q460LrNtQ/EBx6ZdJJnhBk4EwY0snb7TEr
pH5efU2BuCygO3f78DozMxNvvHFFfZ1uw4hobBMSKLKtVcJKmh7r7dvgXtKWdqJf
sUmr2V3LDXTF3r6G5NEorGC4crcSByAvyPatUKRR5t6kAuU8pnWrHnDo+GoSjHy2
vvaUuds6FxHMieFXKrnN8dk6VPpV2PEwzSAk9Ofbq3oZxkIhVGTHjgFPF9tIx3Vu
yo+S4KCP6sxmEt1/p4B5VsCsRU4STbA5jC8qK9vJ1YhMD6tK5FWreaDiWL3wBjf5
xwIDAQAB
-----END PUBLIC KEY-----

That's the solution. Thanks to https://www.virendrachandak.com/techtalk/encryption-using-php-openssl/
// Generate the public key for the private key
$key = openssl_pkey_get_details($privateKey); // $privateKey is created with openssl_pkey_new()
// Save the public key in public.pem file
file_put_contents('public.pem', $key['key']);

Related

Supply PHP function ssh2_auth_pubkey_file with private key as string, not a file

I am trying to pass a string containing content of a RSA private key file to a PHP function ssh2_auth_pubkey_file:
My function looks like this: ssh2_auth_pubkey_file($connection, $ssh_auth_user, $ssh_auth_pub, $ssh_auth_priv, $ssh_auth_pass)
My problem is string $pubkeyfile in my function it is $ssh_auth_priv parameter. Everything works fine when I supply it with a pointer to file for example 'private.pem' but when I pass content of the file as string, I get error.
$ssh_auth_priv = 'private.pem'; works
I have tried:
$ssh_auth_priv = "-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA6Gfo+vYawTT6afHSxBeMYx/s/5nsMHmdckTUj5wPNdG2LhPX
ywGGzyRB9qFAIJAaGmT9fPcgt/IIySOOqzvLGPXbdk15HW71FC8nVQmBYrQXWQnB
........
i3pcqQ2JwSgi9M6rVxDSHQrVVBhiXvAx7Q2B/TEf1BvhtxXdSntUUu63U13VGiOF
1J5RW3EjAeamKOEpaL75xQSSFZdqc7KUG6+cqejhyNkZ4oDk7x+gjuU=
-----END RSA PRIVATE KEY-----
"; does not work
$ssh_auth_priv = <<<KEY-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA6Gfo+vYawTT6afHSxBeMYx/s/5nsMHmdckTUj5wPNdG2LhPX
ywGGzyRB9qFAIJAaGmT9fPcgt/IIySOOqzvLGPXbdk15HW71FC8nVQmBYrQXWQnB
........
i3pcqQ2JwSgi9M6rVxDSHQrVVBhiXvAx7Q2B/TEf1BvhtxXdSntUUu63U13VGiOF
1J5RW3EjAeamKOEpaL75xQSSFZdqc7KUG6+cqejhyNkZ4oDk7x+gjuU=
-----END RSA PRIVATE KEY-----
KEY; Heredoc does not work
I tried file_get_contents to a string, did not work.
My question is how to pass public key as a string, not a pointer to a file.
You will need to write the private key to a temp file and provide the name of that temp file to ssh2_auth_pubkey_file(), eg:
$session = ssh2_connect($host, $port, ['hostkey' => 'ssh-rsa']);
$tempPrivateKeyFileName = tempnam(sys_get_temp_dir(), 'id_rsa');
$fp = fopen($tempPrivateKeyFileName, 'wb+');
fwrite($fp, $yourPrivateKeyContents);
ssh2_auth_pubkey_file($session, $username, $pubkeyfile, $tempPrivateKeyFileName, $passphrase);
unlink($tempPrivateKeyFileName);
It's worth noting writing the private key to a file where it may be read by other processes may be risky depending on your application and environment. At the least you probably want to ensure the private key is encrypted with a passphrase. You may also wish to add a shutdown handler to ensure the temp file is removed if the process exits abnormally. eg:
register_shutdown_function(static function () use ($tempPrivateKeyFileName) {
if (file_exists($tempPrivateKeyFileName)) {
unlink($tempPrivateKeyFileName);
}
});
You can't pass a string for the public key filename argument, it has to be a filename. The function is defined that way.

Public key PEM-like created in c is not accepted in php.Both have openssl

Create public key OpenSSL in c
RSA *rsaKeyPair;
rsaKeyPair = RSA_new();
BIGNUM* exp = NULL;
exp = BN_new();
BN_set_word(exp, 65537);
RSA_generate_key_ex(rsaKeyPair, 2048, exp, NULL);
/* To get the C-string PEM form: */
PEM_write_bio_RSAPublicKey(bio, rsaKeyPair);
keylen = BIO_pending(bio);
pem_key = calloc(keylen+1, 1); /* Null-terminate */
BIO_read(bio, pem_key, keylen);
printf("%s", pem_key); // public key
Then copy-paste in php code.
$key2 = <<<EOT
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAn9VPzLkhDIzPZCA3rYEOttipOjokVQjN7XUjN++n+aYM8PNYp9QY
zlllawj16WE7KchiHjAng5Z+/wgNuz6uxRT4rYcQdjQ+l6JgayVv17vmc4gjuvqN
HeI1vJzs6Ob/kRLRf+QqX84zbSaIWjV1mtVBAyYfG0ln2JW3QvOueGLSV79s0lpE
08YRdXDZ7ohkEWa266RZ5w4970ALVHod8Mq8yPWvQFDpVT7UCMt1Apkz8VfF0zzz
1pxe2NWBJSnuhOkJ3Za+MG1SbiqmHpk6a+wvH9oJYyuR0PV6Tj0baVWenYDY+a6X
m4lmZM18IeLE7RZIqUlfY42hgkviJ45zpQIDAQAB
-----END RSA PUBLIC KEY-----
EOT;
openssl_public_encrypt($c,$d,$key2);
Error is
Warning: openssl_public_encrypt(): key parameter is not a valid public key
Have tried with base64encode in c and decode in php, send over url. Didn't help. Key in 2 ways looks the same.
Here is working locally created public key in php
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyIZzbuW0B7LNQR5JKqCD 6gRKp3DvRkZISaAQDQACSaOpIq7zRUVkgdrYXniECXBmzwGc3Fu5ECkvP0SWbaCI MBh0/YkPKbiQuAnhRwtcVQ8gFKaJ2vdUwp44PkP5pVvfraJPGmfWPAfiL9PyZmRN 1uZfgPznsNVrxqwZvYhApJy57vn4YL4J7kHpK3Peb/K4yTCMyAV/CE8YIe9q0p+L rzFQyIQm2dZ/OI7IU0izQwkbV0ZoMuUGJLhiPMrJ/dkOP3cDLdepJBewald2Hdnp 1aROlXhw0zp4Pj2ERgKkvJ+FnLJ0Se9XxGBBt4e+6gioQfdxvNwKk1oWmsIiyaRE UwIDAQAB -----END PUBLIC KEY-----

Generate public key from saved private key PHP

I have saved Private Key in DB in this format e.g.
-----BEGIN PRIVATE KEY-----
MIICdgIBADA................CHGIdCtZEmTD7sbLUcFCYDbohnqaWf2d+2I
EyC/Hg/tS4d+BQ==
-----END PRIVATE KEY-----
I need extract Public Key from it. openssl_pkey_get_details need resource, but how can I create resource from string?
I tried this
$public_key = openssl_pkey_get_details(openssl_pkey_get_public($private));
Error is openssl_pkey_get_details() expects parameter 1 to be resource, boolean given
EDIT 1:
I'm trying solution from #kbarborak bellow, but $pk_get is false:
$pk_string = "-----BEGIN PRIVATE KEY-----\n".chunk_split($data[4], 64, "\n")."-----END PRIVATE KEY-----\n";
$pk_get = openssl_pkey_get_private($pk_string);
die(var_dump($pk_string));
Output of var_dump is:
string(944) "-----BEGIN PRIVATE KEY-----
MIICjAIBAAINBgkqhkiG9w0BAQEFAAIJKoZIhvcNAQEBAgACggJhMIICXQIBAAKB
gQCvUSrEHS1T5g5z/Yv5ETeXA6CLsdo15Dqk0LevSAXGgbFarSRLfTYtN1/zwqgs
+YWnU/0lZKmzJpmDFRmH/wDeu8o7ptEOrNLgbs6TM85cS4ZmRhJnrOG3G6oBMq5m
UCPw+r06HIuxwyS54/2vY/GGjXpxV59yNUhKh9H9V216YwIDAQABAoGARZAbO/2n
wqaCDZwJMiaWvCEATu2e+8s3SbZXbAFT6VQEn0ocIKfqGz7hn8MZH99SHPbCd1fd
zev+9XglzURtf0sHneq8zT8qT4KYYriRj6dwlyJ6xpf1lLrBqi+eifvZR3+i60Ql
YuBNEXhq/OuSAwV+qTCLYrchUE0jTtX3HWECQQDerLyy1hs2SxlXgLbf80BdIuxB
gqj7QGirYBY/tnPvyLBJWF8RLGjEdPUEaN2hUgkhq2+IQ6lRADKJBQX+1u33AkEA
yY4Hz0rFgjDXx1PLOlX3z6vAFNhtWagcsz+DNp0fwaD1jLVvWnAcAgjJWQ9GBRrC
OGctw6hN92tX19vuKIbr9QJBAIFR1oESIVmlwcD0ysBDOFUezBC7vkG0xVUKB8HW
x3wxlFf0T7ncjbfSqN13vYdL6mu4cZy0lueo8rBqz3GSBz8CQQCTibEzzzL20xQQ
p8jbAfTtgnaPi8G34HRpzbMZ9jg5v74e6u0BEZGe95mT5LdHo0fKi9SXJYs08tGR
hD84UVNFAkAgR+Z0ox6HbNMj5bBIvqEC25mAMnayFyezPZ+/B/IyHDYmQSO9FGsG
/YVA8prMnAZ/YEiCC/2kYA2Ds7GjdJipAgACAAIAAgA=
-----END PRIVATE KEY-----
"
Extracting public key
// Private key as string
$pem_private_key = file_get_contents('mykey.pem');
$private_key = openssl_pkey_get_private($pem_private_key);
// Public key as PEM string
$pem_public_key = openssl_pkey_get_details($private_key)['key'];
// As PHP resource
$public_key = openssl_pkey_get_public($pem_public_key);
echo $pem_public_key;
echo $public_key;
Preparing private key string
$pk_string = "-----BEGIN RSA PRIVATE KEY-----\n".chunk_split($data[4], 64, "\n")."-----END RSA PRIVATE KEY-----\n";
$pk_get = openssl_pkey_get_private($pk_string);
die(var_dump($pk_string));
Use BEGIN RSA PRIVATE KEY instead just BEGIN PRIVATE KEY (same for END).
Hope that's help.
openssl_pkey_get_public() returns false if there's an error. So this is was most likely happens and is passing false to openssl_pkey_get_details(). Debug you $private variable.

Trouble with RSA using phpseclib in PHP

I'm trying to use the RSA implementation in phpseclib, I thought it would be easier to do the code once in a function and re-use the function.
When I've tried texting the code I get a error saying "decryption error"
Testing also made me realise that the ciphertext was different every time the code ran, so I'm clearly doing something wrong there!
The code for the functions is:
require_once "Crypt/RSA.php";
require_once "Crypt/AES.php";
//Function for encrypting with RSA
function rsa_encrypt($string, $public_key)
{
//Create an instance of the RSA cypher and load the key into it
$cipher = new Crypt_RSA();
$cipher->loadKey($public_key);
//Set the encryption mode
$cipher->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
//Return the encrypted version
return base64_encode($cipher->encrypt($string));
}
//Function for decrypting with RSA
function rsa_decrypt($string, $private_key)
{
//Create an instance of the RSA cypher and load the key into it
$cipher = new Crypt_RSA();
$cipher->loadKey($private_key);
//Set the encryption mode
$cipher->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
//Return the decrypted version
return $cipher->decrypt($string);
}
I've tried to test it using the following:
(The keys are just for testing so that's why its hardcoded).
It is in here that everytime the code is run that the value of $ciphertext changes everytime
//Private key
$private_key = "-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HKU0jQjX/n/db9vz/x0i3te/bK LNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJAHmbrNtrUB6ckrYLF75Y50nUN sBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQABAoGAGsiMtAyFu23ac0PdvOuhqm3O/vXAF0Ki zxwBVURfxM6LfiOOcRPe5RSKGTW+Cl7AQVEmMlsa/LtBhLhQ9LNQ5L/4oTmRhCGiZZEmccAdjKsx yMeaxkp+ZHvMxMKQNDgYg1CXqrCrpwwUuMUlA26tfxZ3xSFtFyDTaV9mgDQ1IGECQQCkX9Tum7D1 vQTwbhbYnu7eC4eUOaZeGWSEs2csK7U6vfZ3BzUZW/0tPqcSpQqcNxMtY9TiUsNRj1uM6jX3byp7 AkEA6Ab+wvOTNRtES77OAYG9gHGZZ+iXjQJ/6Z7JehN4p41UbDIf9nNUOLUPL9z5V1uOYnl1CWoo Cw95cdhKXxEAqQJBAIU5Or6tp250ZdVslM27ewSyuY9UblfkIsk/EscFIdzbbDAqwkmsefW6yvTc mU3lgYCPYlKRG8c19tCuX1ENY5MCQAz37x9YW975Ai01ofAFn2DheJCNOINCI4IcROiU1AaRaKmP d6fftFJjFFE5iZovXNr2LOt0yn4rxD7vtuBvY9kCQGyty6YCB6qaD7qXPMhLrLbGajAIWd6ETgxv frK/BJu+buPfDky/g1FhI5R9iMtL1xH0JYLJlaVocU+xSeA9DkY= -----END RSA PRIVATE KEY-----";
//Public key
$public_key = "-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HK U0jQjX/n/db9vz/x0i3te/bKLNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJA HmbrNtrUB6ckrYLF75Y50nUNsBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQAB -----END PUBLIC KEY-----";
//Test out the rsa encryption functions
$plaintext = "This is some plaintext to encrypt";
$ciphertext = rsa_encrypt($plaintext, $public_key);
$decipheredtext = rsa_decrypt($ciphertext, $private_key);
//Echo out results
echo sprintf("<h4>Plaintext for RSA encryption:</h4><p>%s</p><h4>After encryption:</h4><p>%s</p><h4>After decryption:</h4><p>%s</p>", $plaintext, $ciphertext, $decipheredtext);
EDIT:
Sample output is:
Plaintext for RSA encryption:
This is some plaintext we will encrypt
After encryption:
‘˜!ˆ_枦WýF¦E×9&ùš!´jéÓb÷á劀É7J+۪߯׎È㩨ɣ#(÷)ÃX„)÷O‘­Ë±N#Øv«ÓÌPƒ¹—Âî!a¢¦a&Á½Á˜ö‰ºŠCðJ«vW{uAåoOÂXäÞ#÷ï`agÏ:OŒ
After decryption:
//Nothing is returned, it is blank here
I think GigaWatt's answer is the correct one. As for this:
Testing also made me realise that the ciphertext was different every
time the code ran, so I'm clearly doing something wrong there!
PKCS#1 padding adds random bytes so the ciphertext's will always be different. The PKCS#1 algorithm however knows when the plaintext ends and the random bytes begin so it's able to separate the two and return you the result you're wanting.
EDIT: I got it to work after I replaced the spaces in your keys with new lines. eg.
$private_key = "-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HKU0jQjX/n/db9vz/x0i3te/bK
LNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJAHmbrNtrUB6ckrYLF75Y50nUN
sBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQABAoGAGsiMtAyFu23ac0PdvOuhqm3O/vXAF0Ki
zxwBVURfxM6LfiOOcRPe5RSKGTW+Cl7AQVEmMlsa/LtBhLhQ9LNQ5L/4oTmRhCGiZZEmccAdjKsx
yMeaxkp+ZHvMxMKQNDgYg1CXqrCrpwwUuMUlA26tfxZ3xSFtFyDTaV9mgDQ1IGECQQCkX9Tum7D1
vQTwbhbYnu7eC4eUOaZeGWSEs2csK7U6vfZ3BzUZW/0tPqcSpQqcNxMtY9TiUsNRj1uM6jX3byp7
AkEA6Ab+wvOTNRtES77OAYG9gHGZZ+iXjQJ/6Z7JehN4p41UbDIf9nNUOLUPL9z5V1uOYnl1CWoo
Cw95cdhKXxEAqQJBAIU5Or6tp250ZdVslM27ewSyuY9UblfkIsk/EscFIdzbbDAqwkmsefW6yvTc
mU3lgYCPYlKRG8c19tCuX1ENY5MCQAz37x9YW975Ai01ofAFn2DheJCNOINCI4IcROiU1AaRaKmP
d6fftFJjFFE5iZovXNr2LOt0yn4rxD7vtuBvY9kCQGyty6YCB6qaD7qXPMhLrLbGajAIWd6ETgxv
frK/BJu+buPfDky/g1FhI5R9iMtL1xH0JYLJlaVocU+xSeA9DkY=
-----END RSA PRIVATE KEY-----";
//Public key
$public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU+1bLfPmcY7qrF/dTbAtuJlv4R/FVc1WEH9HK
U0jQjX/n/db9vz/x0i3te/bKLNEcwUhBu+PWPnOt/qVURG9BUT6RsCRFUn0CyGiUKoy45o9K/mJA
HmbrNtrUB6ckrYLF75Y50nUNsBVHUDw8yQymmiOBT1gc/KM5s1xTz44LMwIDAQAB
-----END PUBLIC KEY-----";

Decryption with openssl not working

Here is my php script
<?php
$pubfile = fopen("public_key.pem", "r");
$public = fread($pubfile, 8192);
fclose($pubfile);
openssl_get_publickey($public);
$prifile = fopen("private_key.pem", "r");
$private = fread($prifile, 8192);
fclose($prifile);
openssl_get_privatekey($private);
$plaintext = "hello";
openssl_public_encrypt ($plaintext , $crypted , $public);
openssl_private_decrypt($crypted, $decrypted, $private);
echo $decrypted;
?>
my respective public and private keys
private_key.pem
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtwxvPpbvbeJCztWlQCC0w2mQTNICarpIkbFTzlEooXA8Zo30
sN9cdH0Ifa/pIhfx0Lbm+e03Fz5NZelJZ5QRiRfoN9tcb5zYrwvVWx1SR6Zo5moS
S5YEvVSWVpu3D7x3IO2XVtTHlWTTRZk2c+ZuFR3eIwDZbmuOiTmuJMQCJiRd8pqf
4sVCoKzWrUmBuT2pc/sdUQ4IPylGK3yGVyxHNa8gCkBs/zVq3I0Zu7Nx8mW8DTn9
EvP02mkFHynJZgKE0SJftu6Mp9BDl4qR4T8896K+Z1Fi3qx3iK6nkjYjXCYYZB90
6uWiqm7BK/4zbzuc604+BZ4LOPgCsO+aZjx3ywIDAQABAoIBABgTfp5Q4/UkCX2D
BFVG9/+r5trz1HWNQvZGyKseaaWySX+DtGd/OX3o5DED7joZxCibpg3BcbmQ7EU3
RfxlsVjC2DxgkevsSWPcvpdZZ/UcojeMRBPFC6KLeaqymIVtWI3wMjOtkQLJXDCP
UmY0LM6COzGHbxHi+8B44brcPRwe+zJK5ALhPfFEyPsnz9Rw2PLBWceXNeZYN0ja
C699r+a/t0kdJ0+49LLTvFjFECZLArZTK1GZbzACeM/fFl7XvK3tsdAkizys0Dwl
eyDtINkWlg+hzt8uijVY78MvP36RM8k4QIjSX/2p/jupsyZD4I2R9OW+ml9sImok
Ud5YKaECgYEA7xbLLvZ5ddpf/yBSVpGytfU/PzWK5L9cPRbrQPll6iKgimAOTULi
VQAQYaCDL/er/zjVU+a2Kxv1G8yX/LXHXtpSbxIVv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EiQYFlirVq
/jvyUaJg01VjXxp1PKqXwIurF+Eu+W6b4OotLHY0EAPckNiP4XOHMNWd/NG5jV3F
mEzz5Cn9IdMDHSewcA6/rsOqVx0bZOBnSsmVLu9fPsCyrs7eBSXZSlIFKHD39HR6
eQIDAQAB
-----END PUBLIC KEY-----
$crypted returns to me encrypted data however decryption echos nothing.
I did an echo of the result and it comes back as NULL any ideas?
Solved. I don't know what happened but after generating new key pairs script worked. I might have possibly mixed up my key pairs
Using openssl rsa to dump the key contents reveals that the private and public keys you posted above have a different modulus. For a matching keypair, the modulus would be identical in the private and public keys. In other words, you definately mixed up different keys there.
To detect this, OpenSSL proper has the RSA_check_key() function to check whether an RSA keypair is usable, but the PHP wrapper for OpenSSL does not seem to expose that function to the user.
Public key:
% openssl rsa -in public_key.pem -pubin -text
Public-Key: (2048 bit)
Modulus:
00:9c:32:3e:43:fb:5b:55:3a:10:df:40:c1:07:4c:
ba:f3:d8:af:07:4d:60:46:1b:80:11:80:aa:31:e7:
b3:84:4f:02:af:34:0d:dd:61:ac:a3:1d:d0:de:8a:
ed:2d:99:1b:b8:5a:cc:41:d0:4d:e9:98:c3:d7:0d:
24:f6:eb:71:2e:bd:cc:15:16:d7:a8:0c:5e:5d:de:
cd:94:2c:fd:6e:20:5a:43:0d:4b:6e:82:ab:d7:49:
15:c8:43:4f:4d:14:f6:36:b4:27:7e:c1:6c:23:a5:
9f:bc:c2:a2:bb:ad:bf:64:41:d3:f3:40:cc:52:f8:
a5:49:2b:91:6c:d2:ab:7f:03:2a:84:a8:91:6c:cf:
37:3a:5c:5e:a6:4f:17:04:dc:91:8a:6b:f2:a7:16:
8f:85:c1:22:41:81:65:8a:b5:6a:fe:3b:f2:51:a2:
60:d3:55:63:5f:1a:75:3c:aa:97:c0:8b:ab:17:e1:
2e:f9:6e:9b:e0:ea:2d:2c:76:34:10:03:dc:90:d8:
8f:e1:73:87:30:d5:9d:fc:d1:b9:8d:5d:c5:98:4c:
f3:e4:29:fd:21:d3:03:1d:27:b0:70:0e:bf:ae:c3:
aa:57:1d:1b:64:e0:67:4a:c9:95:2e:ef:5f:3e:c0:
b2:ae:ce:de:05:25:d9:4a:52:05:28:70:f7:f4:74:
7a:79
Exponent: 65537 (0x10001)
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnDI+Q/tbVToQ30DBB0y6
89ivB01gRhuAEYCqMeezhE8CrzQN3WGsox3Q3ortLZkbuFrMQdBN6ZjD1w0k9utx
Lr3MFRbXqAxeXd7NlCz9biBaQw1LboKr10kVyENPTRT2NrQnfsFsI6WfvMKiu62/
ZEHT80DMUvilSSuRbNKrfwMqhKiRbM83Olxepk8XBNyRimvypxaPhcEiQYFlirVq
/jvyUaJg01VjXxp1PKqXwIurF+Eu+W6b4OotLHY0EAPckNiP4XOHMNWd/NG5jV3F
mEzz5Cn9IdMDHSewcA6/rsOqVx0bZOBnSsmVLu9fPsCyrs7eBSXZSlIFKHD39HR6
eQIDAQAB
-----END PUBLIC KEY-----
Private key:
% openssl rsa -in private_key.pem -text
Private-Key: (2048 bit)
modulus:
00:b7:0c:6f:3e:96:ef:6d:e2:42:ce:d5:a5:40:20:
b4:c3:69:90:4c:d2:02:6a:ba:48:91:b1:53:ce:51:
28:a1:70:3c:66:8d:f4:b0:df:5c:74:7d:08:7d:af:
e9:22:17:f1:d0:b6:e6:f9:ed:37:17:3e:4d:65:e9:
49:67:94:11:89:17:e8:37:db:5c:6f:9c:d8:af:0b:
d5:5b:1d:52:47:a6:68:e6:6a:12:4b:96:04:bd:54:
96:56:9b:b7:0f:bc:77:20:ed:97:56:d4:c7:95:64:
d3:45:99:36:73:e6:6e:15:1d:de:23:00:d9:6e:6b:
8e:89:39:ae:24:c4:02:26:24:5d:f2:9a:9f:e2:c5:
42:a0:ac:d6:ad:49:81:b9:3d:a9:73:fb:1d:51:0e:
08:3f:29:46:2b:7c:86:57:2c:47:35:af:20:0a:40:
6c:ff:35:6a:dc:8d:19:bb:b3:71:f2:65:bc:0d:39:
fd:12:f3:f4:da:69:05:1f:29:c9:66:02:84:d1:22:
5f:b6:ee:8c:a7:d0:43:97:8a:91:e1:3f:3c:f7:a2:
be:67:51:62:de:ac:77:88:ae:a7:92:36:23:5c:26:
18:64:1f:74:ea:e5:a2:aa:6e:c1:2b:fe:33:6f:3b:
9c:eb:4e:3e:05:9e:0b:38:f8:02:b0:ef:9a:66:3c:
77:cb
publicExponent: 65537 (0x10001)
privateExponent:
18:13:7e:9e:50:e3:f5:24:09:7d:83:04:55:46:f7:
ff:ab:e6:da:f3:d4:75:8d:42:f6:46:c8:ab:1e:69:
a5:b2:49:7f:83:b4:67:7f:39:7d:e8:e4:31:03:ee:
3a:19:c4:28:9b:a6:0d:c1:71:b9:90:ec:45:37:45:
fc:65:b1:58:c2:d8:3c:60:91:eb:ec:49:63:dc:be:
97:59:67:f5:1c:a2:37:8c:44:13:c5:0b:a2:8b:79:
aa:b2:98:85:6d:58:8d:f0:32:33:ad:91:02:c9:5c:
30:8f:52:66:34:2c:ce:82:3b:31:87:6f:11:e2:fb:
c0:78:e1:ba:dc:3d:1c:1e:fb:32:4a:e4:02:e1:3d:
f1:44:c8:fb:27:cf:d4:70:d8:f2:c1:59:c7:97:35:
e6:58:37:48:da:0b:af:7d:af:e6:bf:b7:49:1d:27:
4f:b8:f4:b2:d3:bc:58:c5:10:26:4b:02:b6:53:2b:
51:99:6f:30:02:78:cf:df:16:5e:d7:bc:ad:ed:b1:
d0:24:8b:3c:ac:d0:3c:25:7b:20:ed:20:d9:16:96:
0f:a1:ce:df:2e:8a:35:58:ef:c3:2f:3f:7e:91:33:
c9:38:40:88:d2:5f:fd:a9:fe:3b:a9:b3:26:43:e0:
8d:91:f4:e5:be:9a:5f:6c:22:6a:24:51:de:58:29:
a1
prime1:
00:ef:16:cb:2e:f6:79:75:da:5f:ff:20:52:56:91:
b2:b5:f5:3f:3f:35:8a:e4:bf:5c:3d:16:eb:40:f9:
65:ea:22:a0:8a:60:0e:4d:42:e2:55:00:10:61:a0:
83:2f:f7:ab:ff:38:d5:53:e6:b6:2b:1b:f5:1b:cc:
97:fc:b5:c7:5e:da:52:6f:12:15:be:04:ea:b4:0b:
86:61:3d:21:1a:40:85:b5:44:1a:be:22:46:cf:fa:
f7:0e:71:a2:16:ef:a5:f0:53:f1:c3:e0:90:12:07:
9e:c8:d1:b8:f5:3c:f2:03:b6:8e:0e:cc:16:9b:7a:
3a:69:61:25:a6:d2:43:75:0f
prime2:
00:c3:fe:e9:45:09:20:2d:a2:b3:2c:d8:9d:08:d0:
cd:ab:fe:3f:f3:39:89:a0:c7:ba:19:52:c6:97:e1:
d5:1c:ac:e7:ea:93:d9:7d:cb:48:db:4d:72:68:9e:
4b:6f:23:da:60:3f:f6:d8:c9:43:95:e8:38:03:dc:
d9:20:bc:a4:4b:74:11:a3:f7:b1:dc:7f:bb:7b:16:
00:e2:a4:1c:2e:a5:c9:4f:c0:f9:a0:c9:4d:bc:58:
85:e8:d5:87:bb:b5:89:b5:79:6d:ab:4e:6f:70:15:
dd:47:2e:ac:dd:bc:b5:f9:25:f2:1f:8d:87:e8:7f:
77:d0:02:04:47:5d:fa:e9:85
exponent1:
31:7c:b9:4f:9e:2a:a3:cf:30:81:81:32:29:c2:fc:
87:97:1c:a1:3b:b6:e5:04:0a:d6:86:30:e9:80:f5:
ac:51:c0:83:03:c0:d2:13:1e:ff:d3:04:91:00:85:
46:df:6f:d5:62:f1:1c:ed:ab:15:1f:33:4b:ae:bc:
62:cd:49:58:03:be:7e:48:f3:01:22:c9:65:bc:e0:
f2:ad:91:75:7a:97:72:5e:87:5e:85:7f:bc:ba:c8:
7a:4f:e0:e9:f8:6e:93:60:fc:b0:bb:81:76:89:e7:
3e:cf:d2:05:c8:b8:9b:36:25:0f:41:05:08:e7:72:
c1:af:1e:f8:f9:9a:86:d7
exponent2:
18:45:95:35:cb:6f:84:b7:45:61:24:62:2e:19:4d:
9f:23:19:60:51:9b:cd:a1:6e:56:7c:7d:c3:89:60:
0e:f1:0a:b7:6e:1f:dd:08:33:b9:94:d9:ba:43:00:
2c:7f:50:27:e3:43:fa:8c:d2:25:da:60:35:7a:40:
19:61:c3:26:e4:f4:da:4d:6e:65:54:1c:2f:9e:05:
f6:0b:44:c2:32:1d:c0:16:b1:d3:4b:9a:a8:dc:7d:
98:db:f8:b7:32:40:1f:8d:2e:1b:ac:d6:f8:16:7d:
14:b7:9f:08:c6:b9:f2:64:d4:9b:3b:b7:bc:ff:2c:
23:42:5c:ad:49:e2:2c:d1
coefficient:
00:d4:8d:5d:76:05:df:68:50:f0:22:6d:fc:ba:9b:
b5:06:91:71:f7:dd:c2:8c:ad:15:5a:6c:d6:52:25:
49:f9:ae:fa:1b:b1:67:9f:f2:2e:e6:6d:01:6c:bd:
39:be:52:88:05:de:11:b3:d3:a9:80:36:e2:a9:fc:
00:26:0b:fc:0a:f6:0a:c9:18:db:61:d1:27:3f:e9:
a1:0d:45:b8:c4:63:7f:ef:ea:b1:13:f4:54:5d:9a:
a5:2e:32:9f:bb:bf:88:a0:92:21:18:5e:15:8f:30:
fe:ef:20:cf:1c:7f:ad:29:2c:ff:11:26:a0:10:a0:
3a:af:27:82:e4:24:a9:ad:61
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtwxvPpbvbeJCztWlQCC0w2mQTNICarpIkbFTzlEooXA8Zo30
sN9cdH0Ifa/pIhfx0Lbm+e03Fz5NZelJZ5QRiRfoN9tcb5zYrwvVWx1SR6Zo5moS
S5YEvVSWVpu3D7x3IO2XVtTHlWTTRZk2c+ZuFR3eIwDZbmuOiTmuJMQCJiRd8pqf
4sVCoKzWrUmBuT2pc/sdUQ4IPylGK3yGVyxHNa8gCkBs/zVq3I0Zu7Nx8mW8DTn9
EvP02mkFHynJZgKE0SJftu6Mp9BDl4qR4T8896K+Z1Fi3qx3iK6nkjYjXCYYZB90
6uWiqm7BK/4zbzuc604+BZ4LOPgCsO+aZjx3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