简体   繁体   中英

Implement RSA Public / Private key decryption in .NET to Java / Spring

I'm trying to port an application from .NET to Spring 4 (Java). How do I do a password decryption in Java which was previously encrypted usin .NET RSACryptoServiceProvider? I am from a Java background.

I have a privatekey.xml file from .NET. This is the private key sampel code:

<RSAKeyValue>
<Modulus>dfgkjkdfg99444</Modulus>
<Exponent>ABCD</Exponent>
<P>dadskfkkeej3434j==</P>
<Q>sdakfksa/sdfd/sdf3wfsd==</Q>
<DP>sdERfdsf333==</DP>
<DQ>sdfsd222sdfEWEf==</DQ>
<InverseQ>kadkfimmds+/sdkks+/sdfsdkKKD==</InverseQ>
<D>sdfsadfsadfsadfdsafasdfsadfsadfdsafdsf=</D>
</RSAKeyValue>

Question/s:

Where is the actual private key in this? Can I use it as a salt to decrypt the encrypted text? How can I write a decryption function in Java using this privatekey? Is there already a function for this in Java which accepts these param? Any examples?

I've gone through many theoretical articles about RSA already. But was not helpful in understanding this scenario. So sample codes specifically to handle this scenario in java, counter-part examples in java, links to useful articles etc. will be very helpful.

You've got an XML containing the elements that make up the private key. These elements are base 64 encoded. The base 64 decodes to byte arrays. These byte arrays in turn represent big or little integer encoded numbers. All the values together make up the private key.

Obviously you've altered the values themselves, so it would be rather tricky to go any further than this.

So basically you've got the following steps you need to do:

  • parse the XML;
  • retrieve the encoded elements;
  • base 64 decode those elements into a byte array;
  • convert the arrays into BigInteger values
  • put the values in a RSAPrivateCrtKeySpec
  • feed the spec into an "RSA" KeyFactory .

As you also have the private D value you can also use RSAPrivateyKeySpec but your resulting RSA operation is possibly slower.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM