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 → [https://www.npmjs.com/package/react-native-biometrics?activeTab=readme]
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 Switch mode

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: public_key — OTP 29.0.2 (public_key 1.21.2)

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

Last Post!

bishen28

bishen28

ok working but one typo here, Thanks

Base.decode64!(signature)

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement