I have a public key which is ASN.1 DER formatted shown below :

    0:d=0  hl=3 l= 181 cons: SEQUENCE          
    3:d=1  hl=3 l= 144 cons: SEQUENCE          
    6:d=2  hl=2 l=   0 prim: PRINTABLESTRING   :
    8:d=2  hl=3 l= 139 prim: OCTETSTRING      [HEXDUMP]: <Some Data>
  150:d=1  hl=2 l=  32 prim: OCTET STRING     [HEX DUMP]:<some data>

I have some public key in PEM format. How can change that to the above listed key format?


A PEM file is simply a DER file that's been Base64 encoded. To convert from one to the other you can use openssl with the -inform and -outform arguments. Each one takes one of PEM, DER or NET (a dated Netscape format, which you can ignore).

You can change a key from one format to the other with the openssl rsa command (assuming it's an RSA key, of course):

$ openssl rsa -pubin -inform PEM -in <filename of key in PEM format> -outform DER -out <filename of key in DER format>
writing RSA key

You can then view the ASN.1 encoding of that file with:

$ openssl asn1parse -inform DER -in <filename of DER file>
    0:d=0  hl=3 l= 159 cons: SEQUENCE          
    3:d=1  hl=2 l=  13 cons: SEQUENCE          
    5:d=2  hl=2 l=   9 prim: OBJECT            :rsaEncryption
   16:d=2  hl=2 l=   0 prim: NULL              
   18:d=1  hl=3 l= 141 prim: BIT STRING

Of course, this is of no use to you if your key is not an RSA key, but as your question doesn't stipulate what type of key it is, I've guessed :-)

  • That first sentence was the gold nugget I was looking for. Helped me with a Java programming issue. Thank you very much. – Corin Mar 21 '18 at 16:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.