Elixir ExPublicKey: load rsa public key

I have a pem string of Public_key.pem but not loaded,
pem file included:
pem_string_public = “-----BEGIN RSA PUBLIC KEY-----
MIGMAgEAAoGBAOv0XRPPamsh5g0IaxMMftGR0IlypI9AndJMKyFQ8xbKxrJm1KCm
Of1RtYm3xqyvYwcNUdJz0mTuFhwB5CkHhcwuJCdIUC1J1zC8O9aoz6MYxgfd0l/U
oyYk4NmKtItLT6nOs2YJJO54izX5/osGCGxQaS25PyBkUeXD0LsxSjUFAgMBAAE=
-----END RSA PUBLIC KEY-----”
{:ok, rsa_public_key} = ExPublicKey.load(pem_string_public)

but But I encounter the following error:
no matching action clause to process request.

why?

Are you sure this is caused by the call to ExPublicKey.load/1? It looks much more as if you are using phoenix and do not have set up your action correctly.


Also I’d be happy if you could edit your post and use markdown to format the code blocks. Also please change the tags associated with the post, as it doesn’t have anything to do with deployments.

yes, I will exactly enter this command and give it the error.
I am using phoenix and get this error.

Which command and where do you enter it?

pem_string_public = "-----BEGIN RSA PUBLIC KEY-----
MIGMAgEAAoGBAOv0XRPPamsh5g0IaxMMftGR0IlypI9AndJMKyFQ8xbKxrJm1KCm
Of1RtYm3xqyvYwcNUdJz0mTuFhwB5CkHhcwuJCdIUC1J1zC8O9aoz6MYxgfd0l/U
oyYk4NmKtItLT6nOs2YJJO54izX5/osGCGxQaS25PyBkUeXD0LsxSjUFAgMBAAE=
-----END RSA PUBLIC KEY-----"


{:ok, rsa_public_key} = ExPublicKey.loads(pem_string_public)

Those are not commands, thats two expressions.

And where do you enter them that they cause your error?

In iex or in a file? If the latter does the error happen during compilation or when you start the app or when you do a certain request?

I do still assume that not the code you show us is the problem, but the action that calls it or any other action you hit when the error occurs…

This error happen during running time not compilation.
This code is inside of phoenix api controller. I want to encrypt file with using rsa key that already defined (like pem_string_public in above)و I get an error in loading the pem_string.

I tried your code in iex.

What I do get there is this:

** (MatchError) no match of right hand side value: {:error, %MatchError{term: {:error, {:asn1, {{:unexpected, [{2, <<1, 0, 1>>}]}, [{:"OTP-PUB-KEY", :dec_RSAPublicKey, 2, [file: 'OTP-PUB-KEY.erl', line: 2552]}, {:"OTP-PUB-KEY", :decode, 2, [file: 'OTP-PUB-KEY.erl', line: 1103]}, {:public_key, :der_decode, 2, [file: 'public_key.erl', line: 249]}, {ExPublicKey, :load_pem_entry, 2, [file: 'lib/ex_public_key.ex', line: 122]}, {ExPublicKey, :loads, 2, [file: 'lib/ex_public_key.ex', line: 94]}, {:erl_eval, :do_apply, 6, [file: 'erl_eval.erl', line: 677]}, {:erl_eval, :expr, 5, [file: 'erl_eval.erl', line: 446]}, {:elixir, :eval_forms, 4, [file: 'src/elixir.erl', line: 233]}]}}}}, [{:public_key, :der_decode, 2, [file: 'public_key.erl', line: 253]}, {ExPublicKey, :load_pem_entry, 2, [file: 'lib/ex_public_key.ex', line: 122]}, {ExPublicKey, :loads, 2, [file: 'lib/ex_public_key.ex', line: 94]}, {:erl_eval, :do_apply, 6, [file: 'erl_eval.erl', line: 677]}, {:erl_eval, :expr, 5, [file: 'erl_eval.erl', line: 446]}, {:elixir, :eval_forms, 4, [file: 'src/elixir.erl', line: 233]}, {IEx.Evaluator, :handle_eval, 5, [file: 'lib/iex/evaluator.ex', line: 250]}, {IEx.Evaluator, :do_eval, 3, [file: 'lib/iex/evaluator.ex', line: 230]}]}
    (stdlib) erl_eval.erl:450: :erl_eval.expr/5
    (iex) lib/iex/evaluator.ex:250: IEx.Evaluator.handle_eval/5
    (iex) lib/iex/evaluator.ex:230: IEx.Evaluator.do_eval/3
    (iex) lib/iex/evaluator.ex:208: IEx.Evaluator.eval/3
    (iex) lib/iex/evaluator.ex:94: IEx.Evaluator.loop/1
    (iex) lib/iex/evaluator.ex:24: IEx.Evaluator.init/4

So I digged a bit into it, and I have to say, that the library you are using is buiggy.

It uses erlangs :public_key.pem_decode/1, which returns a list, but your library tries to match this list with an ok-tuple.

So you should stop using that library. Perhaps use the erlang modules directly that ex_crtypto is just a wrapper for?

Sorry didn’t read the code right. my conclusion is wrong.

I use [:ex_crypto, “~> 0.4.0”] and get this an error.

0.4 is old, you should update.

Also:

$ echo -n "-----BEGIN RSA PUBLIC KEY-----\nMIGMAgEAAoGBAOv0XRPPamsh5g0IaxMMftGR0IlypI9AndJMKyFQ8xbKxrJm1KCm\nOf1RtYm3xqyvYwcNUdJz0mTuFhwB5CkHhcwuJCdIUC1J1zC8O9aoz6MYxgfd0l/U\noyYk4NmKtItLT6nOs2YJJO54izX5/osGCGxQaS25PyBkUeXD0LsxSjUFAgMBAAE=\n-----END RSA PUBLIC KEY-----" > foo.pub
$ ssh-keygen -l -f foo.pub
foo.pub is not a public key file.

So perhaps start with using a valid pub-key?

Yes, I even used several other pub-key and got this error.

Have you checked if they are valid using ssh-keygen -l as shown above? If so, can you provide a minified project which causes that error? Please make sure to update your dependencies first.

I changed pub_key:
pem_string_public = “-----BEGIN RSA PUBLIC KEY-----MIGMAgEAAoGBALZRmnDc1CDdcWw5LkDe5l280qafdQ4F9+AoFFOtb3nwZQ/Clm/6hc51ESXD+asWeCOvrACwmSuXCT5YQyiv/lJNqNeoAf3tCwPbYAB3roJ+fTD9EF/UYoACMfa1DNNxRNTo5WaBWs0bjYXJoV8fdQRB4q7p3FFahgEZ/dRQKRgRAgMBAAE=-----END RSA PUBLIC KEY-----”
{:ok, rsa_public_key} = ExPublicKey.loads(pem_string_public)
and get error:

 (exit) an exception was raised:
    ** (MatchError) no match of right hand side value: {:error, "invalid argument"}

I repaired the obvious errors in that file (line-ends after and before head/trailing lines, but still get the message that the key is not valid.

Please show at least a bit of effort and check if the key you are using is valid at all.

1 Like

Thanks a lot. Problem in pub-key generation and resolve it.

2 Likes