PGP public key decoding

Skip to first unread message


Jul 9, 2021, 3:09:11 PMJul 9
to Erlang Questions
Does anyone have an idea how to decode a PGP public key using public_key module?

I am doing this:

1> {ok, B} = file:read_file(PGPPubKeyFile),
2> B1 = re:replace(B, <<"-----BEGIN PGP PUBLIC KEY BLOCK-----">>, <<"-----BEGIN RSA PUBLIC KEY-----">>, [{return, binary}]),
3> B2 = re:replace(B1,  <<"-----END PGP PUBLIC KEY BLOCK-----">>,   <<"-----END RSA PUBLIC KEY-----">>,   [{return, binary}]),
4> B3 = re:replace(B2,  <<"\n.+(\n-----END)">>,    <<"\\1">>, [{return, binary}]), % Remove checksum
5> B4 = re:replace(B3,  <<"\nVersion:.+\n\r?\n">>, <<"\n">>,  [{return, binary}]), % Remove version
6> [Entry] = public_key:pem_decode(B4),
7> public_key:pem_entry_decode(Entry).                                                
** exception error: no match of right hand side value {error,{asn1,{{wrong_tag,{{expected,16},
    in function  public_key:der_decode/2 (public_key.erl, line 322)

Max Lapshin

Jul 9, 2021, 7:43:44 PMJul 9
to Serge, Erlang Questions

1. maybe it is not base64, but radix64 and it could be another thing?
2. is it really ASN?

Stanislav Ledenev

Jul 10, 2021, 6:59:21 AMJul 10
to Serge, Erlang Questions
Your Entry is already decoded, so no need to decode it again.
It must be something like {'RSAPublicKey',.....}

пт, 9 июл. 2021 г. в 22:09, Serge <>:


Jul 11, 2021, 9:12:54 PMJul 11
to Max Lapshin, Erlang Questions
I followed the key re-formatting advice mentioned here:

However, as shown, public_key:pem_entry_decode/1 fails.


Jul 11, 2021, 9:15:09 PMJul 11
to Stanislav Ledenev, Erlang Questions
Oh, I see. I thought the two functions public_key:pem_decode/1 and public_key:pem_entry_decode/1 need to be used in succession to decode the entry.
Reply all
Reply to author
0 new messages