views:

472

answers:

1

I am trying to decrypt a string with RSA. It was encrypted in C# on the iPhone and I have the private key. This seems like a silly problem, but all of the examples I have seen show generating the private key. I have the private key (it is a byte[] of hex). It using PKCS#1 padding. The part I cannot figure out how to do is create a java.security.Key object with the private key I already have.

Do I need to have them give me the private key in 2 parts...modulus and exponent?

Thanks in advance.

+1  A: 

You need to go through a RSAPrivateKeySpec. Here's an example (based on this):

        BigInteger n = new BigInteger(nBytes);
        BigInteger p = new BigInteger(pBytes);
        RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(n, p);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        Key privateKey = kf.generatePrivate(privateSpec);
Laurence Gonsalves
I guess my problem is that I was given the private key as a byte array in hex...0x12, 0xe3, etc. I guess the solution is that I need the modulus and exponent.
Meredith L. Patterson
So, it does...I will give it a try. Thanks for the help Meredith and Laurence.