Sunday, 3 April 2022

openSSL: How to generate RSA private, public key pair

Syntax

openssl genrsa -des3 -out {FILE_NAME} {KEY_SIZE}

 

Example

openssl genrsa -des3 -out my_rsa_key_pair 2048

 Option -des3 make sure that the pem file generated in the above step will be protected by a passphrase.

 

$openssl genrsa -des3 -out my_rsa_key_pair 2048
Generating RSA private key, 2048 bit long modulus
..................................+++
..........................+++
e is 65537 (0x10001)
Enter pass phrase for my_rsa_key_pair:
Verifying - Enter pass phrase for my_rsa_key_pair:
$
$ls
my_rsa_key_pair

 


Export private key to a pem file

Export private key to privateKey.pem file by executing below commands. 

openssl pkcs8 -topk8 -inform PEM -in my_rsa_key_pair -outform PEM -out privateKey.pem -nocrypt

 

$openssl pkcs8 -topk8 -inform PEM -in my_rsa_key_pair -outform PEM -out privateKey.pem -nocrypt
Enter pass phrase for my_rsa_key_pair:
$
$ls
my_rsa_key_pair	privateKey.pem
$
$cat privateKey.pem 
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCggQ7vAqJvYhYv
v7V6GeoDS258BeANEShKWslGqN8V6R2xLMB/dFyyDt50YBFb7gScliZQmNjFDjo/
EbtaETGxxTv5958TOA4VfurEwHrzMbyChTZwAKlBKhvyyVFN2ZATLR0jMg97g6f1
PdDvCYFIbk15RMKjSXSEz7xG8K6hKvkdoFqk4MORHvvC1e438+KqwhwSgVCa5D0E
UQ0Hm6aUOjRrVwn7iuG/pktndPWMWfqcS9yGUIdOlg2F1rZQdorej0O9MCVsIEON
nGXhStwnGJMchGcraFsEsS8JROmVcZQV+8ebPM2R+hmIQi1xAAw4stWQ4EJkJq2b
SHop9ekHAgMBAAECggEASqyg4QyNvFqbTe3Ypg1Gde/zYFpvakTwRN/GQ0ywNgZ2
XQcgLGxb8C/Ayc4kZWvw01V8UAU83TmIWe2LCBV11GqaQ+s0JIHw84lP+ug+i8U5
GlSWBjgYhwvrYuLNI/Yz9OgJZWWWFHYlf5EaEw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B7FGsBs/JNuip4wR4pk=
-----END PRIVATE KEY-----

 Export public key to a pem file

Export public key to publicKey.pem file by executing below command.

openssl rsa -in my_rsa_key_pair -outform PEM -pubout -out publicKey.pem

$openssl rsa -in my_rsa_key_pair -outform PEM -pubout -out publicKey.pem
Enter pass phrase for my_rsa_key_pair:
writing RSA key
$
$ls
my_rsa_key_pair	publicKey.pem
$
$cat publicKey.pem 
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoIEO7wKib2IWL7+1ehnq
A0tufAXgDREoSlrJRqjfFekdsSzAf3Rcsg7edGARW+4EnJYmUJjYxQ46PxG7WhEx
scU7+fefEzgOFX7qxMB68zG8goU2cACpQSob8slRTdmQEy0dIzIPe4On9T3Q7wmB
SG5NeUTCo0l0hM+8RvCuoSr5HaBapODDkR77wtXuN/PiqsIcEoFQmuQ9BFENB5um
lDo0a1cJ+4rhv6ZLZ3T1jFn6nEvchlCHTpYNhda2UHaK3o9DvTAlbCBDjZxl4Urc
JxiTHIRnK2hbBLEvCUTplXGUFfvHmzzNkfoZiEItcQAMOLLVkOBCZCatm0h6KfXp
BwIDAQAB
-----END PUBLIC KEY-----



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment