Perseids
Perseids

Reputation: 13250

How do I get an instance of RSAPrivateKey

How do I create an instance of java.security.interfaces.RSAPrivateKey given RSA's d, e and n (let's assume as BigIntegers). (Here d denotes the private exponent, e the public exponent and n=pq the RSA modulus.)

I thought this would be really simple but I can't find anything in the documentation or the internet in general.

If that it is any help, I already have BouncyCastle installed.

Edit to clarify: I'm looking for a class that implements the interface and takes d, e and/or n as arguments to the constructor (or argument to a factory function, etc.) as opposed to creating a new, random key or reading the key from a file in some PKCS* format.

Upvotes: 3

Views: 9510

Answers (1)

Keith
Keith

Reputation: 4184

Well, here is how you can construct one given the private exponent and modulus ( all that is required for the private key ):

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

    RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(
            new BigInteger("57791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb1", 16),
            new BigInteger("57791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb157791d5430d593164082036ad8b29fb1", 16)
    );

    RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);

Upvotes: 7

Related Questions