bishen28

bishen28

Issue with ExPublicKey Expublic.load()

Hello guys i am stuck at an issue please help me :pray: :pray: :pray:

for the biometric authentication in my app, from front-end i am getting public key, signature.

for generating key pair, front end using this library → [react-native-biometrics - npm]
and this algorithms → Algorithmic used by above library

example
like i am getting following public key and signature form front end
public_key : MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsdBu6w2Var6TJHT8TQ1WdDpMu4VzSrDRP8DjY0WI9ddZvQSvjTdUQ9qm3fyZufZpmhPXfjkmg+f/cbJh+m+Zzhf093bcqVVnmEl+an/FTPMaOy2jfYNbxwZDuYCUxJzewGDa74vXCYB2sPgqZikf3UlhI0ZPO8tbdhleGoiQXOcXloAELQ+ebQ6MDBr+QDqNedPW+hLultTClH56v88Tp85PT6HWnPjVQUAWR7QeL+U8mFZOQALBruQvqfB3BwromhTkTB5XtCy1WCGBtzs21CTok4+pWp5QM/E3IUv1B8dtZDqjMIyGhUnM7pFmgL58UYV1unhyMH/cM+ffzlJWIQIDAQAB

Signature : it is encryption of text = "1675256878678"

DQ6vLV4cRAMcCkpT0MTMHPUc8IoMvcckhjY9Fyirq/zX1Ej851g+IojotyPMqSFKbRqrpE3F2OTI717fcAxFRMCFYlcuf1mYjniCJtGTkxTE6m6GNy+ApX5ssUSb3pZajXcLw/YnKvnbiYtsnL0mFgZ27krrp8ZxEf9iDFBJRZW7JVxjaeGqe5jIN64+NODgR+OvMvHPLpNjKIrZleXwQwbOpWE1q9EIhWmApc2bDdUFeaa5RtGOLI9WNkitkhW/Dsk5FNiZOW7oo85IE65cMJ09XTKHCUmYQnhwtdKibKK84koKXELvBat9/aWe+GBdQJQa6mNFfEPnvcmpXDu3tQ==

in the backend i am using elixir ExPublicKey
so the issue that, the public key i am getting from the front end is not getting load as
the public key object type, something like that

{:ok, public_key} = ExPublicKey.loads(public_key)
iex(64)> ExPublicKey.loads(public_key)
{:error, "invalid argument"}

the desired results should be like this

iex(67)> {:ok, public_key} = ExPublicKey.loads(public_key)
{:ok, #ExPublicKey.RSAPublicKey<
   fingerprint_sha256=                                                                                                   bc                                                                                                         
     31
     a4
     6b
     51
     67
     68
     b2
     63
     60
     66
     73
     39
     ad
     c8
     66
     1e
     07
     9d
     0c
     02
     e6
     31
     23
     16
     9d
     a3
     78
     4a
     04
     ef
     e8>}

{ok, "1675256878678"} = ExPublicKey.decrypt_public(params.signature, public_key, [])

Help me out here. or any alternative solution in elixir for the above encryption/decryption

Marked As Solved

al2o3cr

al2o3cr

I was able to parse that string into an RSAPublicKey with:

iex(7)> {:ok, public_key} = pk_in |> Base.decode64!() |> ExPublicKey.RSAPublicKey.decode_der()
{:ok, #ExPublicKey.RSAPublicKey<
   fingerprint_sha256=
     39
     9f
     25
     36
     c0
     04
     e8
     10
     d7
     fa
     a8
     bb
     c2
     90
     49
     34
     c5
     c9
     32
     9c
     74
     b1
     1b
     62
     11
     d9
     29
     a3
     12
     2b
     98
     f4>}

which produces a plausible public key (the exponent is 65537) but with a different fingerprint…

It does validate the supplied signature, though:

pk_in = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsdBu6w2Var6TJHT8TQ1WdDpMu4VzSrDRP8DjY0WI9ddZvQSvjTdUQ9qm3fyZufZpmhPXfjkmg+f/cbJh+m+Zzhf093bcqVVnmEl+an/FTPMaOy2jfYNbxwZDuYCUxJzewGDa74vXCYB2sPgqZikf3UlhI0ZPO8tbdhleGoiQXOcXloAELQ+ebQ6MDBr+QDqNedPW+hLultTClH56v88Tp85PT6HWnPjVQUAWR7QeL+U8mFZOQALBruQvqfB3BwromhTkTB5XtCy1WCGBtzs21CTok4+pWp5QM/E3IUv1B8dtZDqjMIyGhUnM7pFmgL58UYV1unhyMH/cM+ffzlJWIQIDAQAB"
text = "1675256878678"
signature = "DQ6vLV4cRAMcCkpT0MTMHPUc8IoMvcckhjY9Fyirq/zX1Ej851g+IojotyPMqSFKbRqrpE3F2OTI717fcAxFRMCFYlcuf1mYjniCJtGTkxTE6m6GNy+ApX5ssUSb3pZajXcLw/YnKvnbiYtsnL0mFgZ27krrp8ZxEf9iDFBJRZW7JVxjaeGqe5jIN64+NODgR+OvMvHPLpNjKIrZleXwQwbOpWE1q9EIhWmApc2bDdUFeaa5RtGOLI9WNkitkhW/Dsk5FNiZOW7oo85IE65cMJ09XTKHCUmYQnhwtdKibKK84koKXELvBat9/aWe+GBdQJQa6mNFfEPnvcmpXDu3tQ=="

{:ok, public_key} = pk_in |> Base.decode64!() |> ExPublicKey.RSAPublicKey.decode_der()

{:ok, valid?} = ExPublicKey.verify(text, Base64.decode64!(signature), public_key)

Also Liked

jerdew

jerdew

That looks like a helpful error message at least, look at the ExPublicKey.RSAPublicKey module, perhaps decode_der?. Also, you’ll probably want to Base.decode64 the key you have, because these functions likely want it in a raw binary format.

Also might be a useful function here: Erlang -- public_key

Keep trying and search for examples that do work that you can get a hint from.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement