5

I'm unable to "pem_entry_decode" GPG public key in Erlang. Public key generated through OpenSSL works fine. I've fixed GPG key as suggested in Erlang - Importing GPG Public Key.

#!/usr/local/bin/escript

main(_) ->
  [application:start(X) || X <- [crypto, public_key, ssl]],
  Msg = list_to_binary("Hello World!"),
  %{ok, FileContents} = file:read_file("public_openssl.pem"),
  {ok, FileContents} = file:read_file("public_gpg.asc"),
  show(FileContents),
  [Entry] = public_key:pem_decode(FileContents),
  show(Entry),
  Key = public_key:pem_entry_decode(Entry),
  show(Key),
  EM = public_key:encrypt_public(Msg, Key),
  show(EM).

show(Something) ->
  io:format("~p~n", [Something]).

Error:

escript: exception error: no match of right hand side value 
                 {error,
                     {asn1,
                         {wrong_tag,
                             {{expected,16},{got,131097,{131097,<<"\r">>}}}}}}
  in function  public_key:der_decode/2 (public_key.erl, line 170)
  in call from erl_eval:do_apply/6 (erl_eval.erl, line 572)
  in call from erl_eval:expr/5 (erl_eval.erl, line 367)
  in call from escript:eval_exprs/5 (escript.erl, line 850)
  in call from erl_eval:local_func/5 (erl_eval.erl, line 470)
  in call from escript:interpret/4 (escript.erl, line 768)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_it/1 
Community
  • 1
  • 1
ram
  • 559
  • 1
  • 5
  • 15
  • Is the source code consistent with the error stack ? The error stack mentions public_key:der_decode whereas the source code mentions public_key:pem_decode... – user803422 Mar 28 '13 at 11:57
  • I checked in public_key.erl. `der_decode` is called by `pem_entry_decode`. Not sure why it isn't showing entire stack. – ram Mar 28 '13 at 12:49
  • 1
    I advise you to try to reproduce this issue in a newer Erlang release, say, in R16B. There were news that it is now more friendly to various DER encoded stuff, even slightly malformed. – Keynslug Jun 10 '13 at 12:48

1 Answers1

1

From your:

{ok, FileContents} = file:read_file("public_gpg.asc")

FileContents, select first 16 characters.