Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reconstructing a RSA private key from a modulus and private exponent

843810Jan 25 2002 — edited Jan 29 2002

I have a problem with re-creating a private key from a modulus and private exponent.

In the code bellow I am using two created keys ('thePrivateKey' and 'thePublicKey',
of type RSAPrivateKey and RSAPublicKey) to get the modulus and private exponent, this is
just for the my test program, in my final implementation I will not have access to
these and will only have the modulus and private exponent, this is why I am doing things this way.

The strange thing is that I can get this to work for a public key, eg:

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec ks_public = new RSAPublicKeySpec(((RSAPublicKey)thePublicKey).getModulus(),((RSAPublicKey)thePublicKey).getPublicExponent());
PublicKey publicReconstructedKey = keyFactory.generatePublic(ks_public);

byte[] r_public = ((java.security.interfaces.RSAPublicKey)publicReconstructedKey).getEncoded();

I can then print out 'r_public' ok.

When I do the same thing with the private key with the following:

KeySpec ks_private = new RSAPrivateKeySpec(((RSAPrivateKey)thePrivateKey).getModulus(),((RSAPrivateKey)thePrivateKey).getPrivateExponent());
PrivateKey privateReconstructedKey = keyFactory.generatePrivate(ks_private);

byte[] r_private = ((RSAPrivateKey)privateReconstructedKey).getEncoded();

I get the following error:

java.lang.ArrayIndexOutOfBoundsException: 0
at com.sun.rsajca.JSA_RSAPrivateKey.getEncoded([DashoPro-V1.2-120198])
at GenKeysThread.run(GenKeysThread.java:141)

The privateKey privateReconstructedKey is a valid key though, because I can get the exponent and modulus from it
and use it for signing data. It just won't give me the encoded format.

Any ideas/solutions would be appreciated!

Thanks,

Martin.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 26 2002
Added on Jan 25 2002
1 comment
984 views