title: Porting .Net RSA xml keys to Java
body: <p>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?</p>
  
  <p>Public key looks something like this:</p>
  
  <pre><code>&lt;RSAKeyValue&gt;
      &lt;Modulus&gt;jHIxcGzzpByFv...pvhxFnP0ssmlBfMALis&lt;/Modulus&gt;
      &lt;Exponent&gt;AQAB&lt;/Exponent&gt;
  &lt;/RSAKeyValue&gt;
  </code></pre>
  
  <p>Private key:</p>
  
  <pre><code>&lt;RSAKeyValue&gt;
      &lt;Modulus&gt;4hjg1ibWXHIlH...ssmlBfMAListzrgk=&lt;/Modulus&gt;
      &lt;Exponent&gt;AQAB&lt;/Exponent&gt;
      &lt;P&gt;8QZCtrmJcr9uW7VRex+diH...jLHV5StmuBs1+vZZAQ==&lt;/P&gt;
      &lt;Q&gt;8CUvJTv...yeDszMWNCQ==&lt;/Q&gt;
      &lt;DP&gt;elh2Nv...cygE3657AQ==&lt;/DP&gt;
      &lt;DQ&gt;MBUh5XC...+PfiMfX0EQ==&lt;/DQ&gt;
      &lt;InverseQ&gt;oxvsj4WCbQ....LyjggXg==&lt;/InverseQ&gt;
      &lt;D&gt;KrhmqzAVasx...uxQ5VGZmZ6yOAE=&lt;/D&gt;
  &lt;/RSAKeyValue&gt;
  </code></pre>
  
  <p>I have written a bit of code to encrypt data but I am not sure if its correct.</p>
  
  <pre><code>        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);
  </code></pre>
  
  <p>How can I make a private key from the xml to decrypt the data?</p>