Skip to content

Instantly share code, notes, and snippets.

@nghiaht
Created September 13, 2018 03:30
Show Gist options
  • Save nghiaht/224f7fe04ea591c6d2fddbee6c173379 to your computer and use it in GitHub Desktop.
Save nghiaht/224f7fe04ea591c6d2fddbee6c173379 to your computer and use it in GitHub Desktop.
Generate RSA keypair (public, private + pkcs8) using openssl command
# Private key
openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
# Public key
openssl rsa -pubout -in private.pem -out public_key.pem
# Private key in pkcs8 format (for Java maybe :D)
openssl pkcs8 -topk8 -in private.pem -out private_key.pem
## nocrypt (Private key does have no password)
openssl pkcs8 -topk8 -in private.pem -nocrypt -out private_key.pem
@jmxnzo
Copy link

jmxnzo commented Jun 26, 2023

It seems like the first and last command do exactly the same, because openssl changed their default format to pkcs#8, there is no convertion needed any longer.
$ openssl asn1parse -in private_key.pem -inform PEM
0:d=0 hl=4 l=1213 cons: SEQUENCE
4:d=1 hl=2 l= 1 prim: INTEGER :00
7:d=1 hl=2 l= 13 cons: SEQUENCE
9:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
20:d=2 hl=2 l= 0 prim: NULL
22:d=1 hl=4 l=1191 prim: OCTET STRING

$ openssl asn1parse -in private.pem -inform PEM
0:d=0 hl=4 l=1213 cons: SEQUENCE
4:d=1 hl=2 l= 1 prim: INTEGER :00
7:d=1 hl=2 l= 13 cons: SEQUENCE
9:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
20:d=2 hl=2 l= 0 prim: NULL
22:d=1 hl=4 l=1191 prim: OCTET STRING

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment