2

I call API that returns 2 public keys (as string), each consist of 512bits:

588506d0c604d8270ac4de9fdc520abe4779128ff5b7940d38fcd13d5e5fd07f

455c2c7b4e4a873c40f46b8e2bdfd90214591c3110b3c7ab7458818af3c59649

What i need to do, is to create PublicKey object from them in order to sign data with. ( Each key for different data )

However, what i am trying to do throws error:

KeyFactory kf = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyContent));
RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(keySpecX509);

The error is:

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: DerInputStream.getLength(): lengthTag=79, too big.

(Each key has different "lengthTag" in eror )

This publicKeys should be correct ( i was assured the API returns correct keys )

Am i misunderstanding something? Did i do any mistake? I am unable to figure it out, searching depths of google shows same method i used.

I appreciate all help or hints!

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
Darlyn
  • 4,715
  • 12
  • 40
  • 90

1 Answers1

2

You are attempting to Base64 decode a hex encoded string. Instead you need to decode hex values to byte[]. Take a look at this answer to understand how.

However signing with RSA is done with a private key. I'm not sure how you plan to use two public keys to sign something. You can check How does RSA signature verification work?, perhaps I'm misunderstanding something.

Moreover you have two 64 character strings returned by the API. Assuming they are hex encoded each of them will convert to 32 bytes or 256 bits. Your post title mentions 512 bits so it looks like these are possibly two halves of a single key... Something is wrong with what you are trying to do.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Thanks for answer! I will need to ask for more specifications, however it may be that i misunderstood it and those are really 2 32 bytes long keys. However i tried to decode hex to byte and pass this array of bytes to `new X509EncodedKeySpec();` and generate public key from this, however the error ramained the same. The only thing that has changed is lengthTag in error mesage – Darlyn Jun 11 '20 at 23:03