keyfactory does not accept RSAPrivateKey for private key encryption

  • Thread starter Marco A. Cruz Quevedo
  • Start date
M

Marco A. Cruz Quevedo

Hi everybody:

I have the following code:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

X509EncodedKeySpec publicKeySpec = new
X509EncodedKeySpec(encodedPublicKey);
System.out.println("\tKey Factory Algorithm =
"+keyFactory.getAlgorithm());
System.out.println("\tKey Factory Provider =
"+keyFactory.getProvider());

PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
System.out.println("\t*** Public key in key factory\n\n");

PKCS8EncodedKeySpec privateKeySpec = new
PKCS8EncodedKeySpec(encodedPrivateKey);
System.out.println("\tEncoded KeySpec Format=
"+privateKeySpec.getFormat());
//-- *** error !
RSAPrivateKey mc_privkey =
keyFactory.generatePrivate(privateKeySpec);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

And when this code is run, I get the following error:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

java.security.InvalidKeyException: IOException : DER input,
Integer tag error
java.security.spec.InvalidKeySpecException:
java.security.InvalidKeyException: I
OException : DER input, Integer tag error
at
sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(Unknown Source)
at java.security.KeyFactory.generatePrivate(Unknown Source)
at encripta.main(encripta.java:99)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

It seems the keyfactory does not accept the RSAPrivateKey object, Why?
What is missing or wrong?

Thank you in advance

Marco.
---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top