0

My server side(C# or .Net) people giving me a RSA Key pair in xml format, but the problem is I am not able to use given keys in my Java/ Android code and it always throwing InValidKeySpec exception.

Somehow I managed RSAPublic key conversion into Java understandable RSAPublic key format like this way -

String Modoutput = "pgFkNu7tN3K8VCxxvKMFwqaRJ6I158/aihg1J1p13P5HvVz8Pn2oC7hfdhujlQxHPsV/b8Rc3Snq5KGmC4VBnw==";
            String Expoutput = "AQAB";

            byte[] exponentBytes = Base64.decode(Expoutput);
            byte[] modulusBytes =  Base64.decode(Modoutput);

            BigInteger e = new BigInteger(1, exponentBytes);
            BigInteger m = new BigInteger(1, modulusBytes);
            RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
            PublicKey mServerPublicKey = rsaKeyFactory.generatePublic(keySpec);

But not able to convert given RSAPrivate (C#) key into Java/ Android understandable. I tried this link, and this

I am looking into this but not getting any success, need help from the experts.

Happy coding :-)

Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63

2 Answers2

0

Assuming you have the private exponent in your XML, this is the equivalent code for generating the private key in Java. For Android replace the base64 encoding part

String privateExponentB64 = "...";
String modulusB64 = "...";

byte[] privateExponentBytes = Base64.getDecoder().decode(privateExponentB64);
byte[] modulusBytes = Base64.getDecoder().decode(modulusB64);

BigInteger privateExponent = new BigInteger(1, privateExponentBytes);
BigInteger modulus = new BigInteger(1, modulusBytes);
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, privateExponen);
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • and is okay but what about other part like

    , , , , , ?? are those not necessary to generate a Java understandable Private key??

    – Bajrang Hudda Feb 02 '18 at 07:15
  • the *privateExponent* (not the public) and the modulus are enough to create the key. Use value for the privateExponent – pedrofb Feb 02 '18 at 07:18
  • when I am trying to use this private key, getting BadPaddingexception – Bajrang Hudda Feb 05 '18 at 10:27
  • The error may be due to many reasons, which may have nothing to do with the code I have provided. Please update your question or create a new one with the code you are using to use the private key, as well as the complete trace of the error – pedrofb Feb 05 '18 at 10:41
-1

you may take a reference from this link

How do I use a C# generated RSA public key in Android?

as it seems it will be helpful to you

diksha
  • 109
  • 8
  • Public key is okay, but the problem is I want Private Key as well. – Bajrang Hudda Feb 02 '18 at 05:33
  • if you want to convert given key to understandable form ...... hope this will help you...https://stackoverflow.com/questions/22214966/convert-java-security-keypair-to-net-rsacryptoserviceprovider – diksha Feb 02 '18 at 05:42
  • 1
    Please do not post a link as an answer. Include here the relevant parts. See https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – pedrofb Feb 02 '18 at 07:14