Porting .Net RSA xml keys to Java
Solution 1
Is that decoder
in your example doing the Base64 decoding? It looks like you might be relying on sun.misc.BASE64Decoder
and it's generally not a good idea to depend on those internal classes (other JVM's won't have it for instance). You could use Apache Commons Codec that has a Base64 class to decode with. Here's the rest of what you need though for RSA encryption and decryption:
byte[] expBytes = Base64.decodeBase64(exponentElem.getText().trim()));
byte[] modBytes = Base64.decodeBase64(modulusElem.getText().trim());
byte[] dBytes = Base64.decodeBase64(dElem.getText().trim());
BigInteger modules = new BigInteger(1, modBytes);
BigInteger exponent = new BigInteger(1, expBytes);
BigInteger d = new BigInteger(1, dBytes);
KeyFactory factory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA");
String input = "test";
RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent);
PublicKey pubKey = factory.generatePublic(pubSpec);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
System.out.println("encrypted: " + new String(encrypted));
RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);
PrivateKey privKey = factory.generatePrivate(privSpec);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("decrypted: " + new String(decrypted));
Solution 2
Try to use
RSAPrivateKeySpec privSpec = new RSAPrivateCrtKeySpec(iModulus,iExponentBytes,iDBytes,iPBytes,iQBytes,iDPBytes,iDQBytes,iInverseQBytes)
and add all private key component
Related videos on Youtube
Comments
-
dvl about 4 years
I have private and public keys from the .Net system in the xml format. I have to use this keys to perform encryption/decryption in Java. Is there any way to do it?
Public key looks something like this:
<RSAKeyValue> <Modulus>jHIxcGzzpByFv...pvhxFnP0ssmlBfMALis</Modulus> <Exponent>AQAB</Exponent> </RSAKeyValue>
Private key:
<RSAKeyValue> <Modulus>4hjg1ibWXHIlH...ssmlBfMAListzrgk=</Modulus> <Exponent>AQAB</Exponent> <P>8QZCtrmJcr9uW7VRex+diH...jLHV5StmuBs1+vZZAQ==</P> <Q>8CUvJTv...yeDszMWNCQ==</Q> <DP>elh2Nv...cygE3657AQ==</DP> <DQ>MBUh5XC...+PfiMfX0EQ==</DQ> <InverseQ>oxvsj4WCbQ....LyjggXg==</InverseQ> <D>KrhmqzAVasx...uxQ5VGZmZ6yOAE=</D> </RSAKeyValue>
I have written a bit of code to encrypt data but I am not sure if its correct.
Element modulusElem = root.getChild("Modulus"); Element exponentElem = root.getChild("Exponent"); byte[] expBytes = decoder.decodeBuffer(exponentElem.getText().trim()); byte[] modBytes = decoder.decodeBuffer(modulusElem.getText().trim()); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(1, modBytes), new BigInteger(1, expBytes)); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pubKey = fact.generatePublic(keySpec);
How can I make a private key from the xml to decrypt the data?
-
dvl about 13 yearsThanks. It worked well. Just a bit curious about why we didn't use any of the other elements in Private Key Xml? (P, Q, DP, DQ etc.)
-
WhiteFang34 about 13 yearsNo problem. Those other elements are the original primes (p and q) used for key generation as well as some precomputed values. They can be used to do decryption in an alternate way (using Chinese Remainder Theorem) that's faster. You can actually use it in Java by creating a
RSAPrivateCrtKeySpec
that takes all of the elements. -
ecem about 11 yearsHey, just a quick question, what should I use for parsing this public/private key xml file? I want something really easy to use, and I feel like I'm drown in the sea of xml parsers.
-
Meteorite almost 9 yearsDoes anybode know, how to do the same using BouncyCastle in Java ?
-
user2018726 about 7 yearsWhiteFang34, you are awesome :) this solved my problem.
-
jk2K over 2 yearsI got error:
BadPaddingException: Message is larger than modulus
-
jk2K over 2 yearssorry, I made a mistake, My xml file is outdated and is not the correct keys info. Now my application is working properly. thank you,