2
votes

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!

1
They are hex not base64. see stackoverflow.com/q/140131/1820553kelalaka
Those are not RSA public keys and they are not 512 bits. They might be elliptic curve public key(s).President James K. Polk

1 Answers

2
votes

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.