How to convert SSH public key from PEM to DER format?
21,525
OpenSSH public key format is different from PEM format. You have to run ssh-keygen
to convert it.
ssh-keygen -f ~/.ssh/id_rsa.pub -e -m PEM >pubkey.pem
Then convert it to DER format using openssl rsa
.
openssl rsa -RSAPublicKey_in -in pubkey.pem -inform PEM -outform DER -out ~/.ssh/id_rsa.pub.der -RSAPublicKey_out
Note that you also have to specify -RSAPublicKey_in
(not -pubin
) and -RSAPublicKey_out
to keep it to be RSA Public Key (PKCS#1).
Related videos on Youtube
Author by
Edward
Updated on September 18, 2022Comments
-
Edward over 1 year
I'd like to convert an existing public key (generated using
ssh-keygen
) fromPEM
-format toDER
-formatI tried:
openssl rsa -in ~/.ssh/id_rsa.pub -inform PEM -pubin -outform DER -out ~/.ssh/id_rsa.pub.der
Unfortunately this gives the following error:
unable to load Public Key 139946985690784:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:703:Expecting: PUBLIC KEY
The file
~/.ssh/id_rsa.pub
contains a public key;cat ~/.ssh/id_rsa.pub
gives:ssh-rsa ...... user@machine
What am I doing wrong?
-
nakajuice about 6 yearsDoesn't work in MacOS:
unknown option -RSAPublicKey_in
-
Leo over 4 yearsWorks on MacOS 10.14.6 with openssl 2.6.5