简体   繁体   中英

RSA encryption in C#: What part defines the public key?

I've generated a new public/private key pair and exported it as an XML string:

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048);  
string publicPrivateKey = RSA.ToXmlString(true);

The XML string in publicPrivateKey looks like this (strings are shortened for readability):

<RSAKeyValue>
   <Modulus>t6tLd1Wi7PEkwPfx9KGP1Ps/5F2saXnOsCE2U....</Modulus>
   <Exponent>AQAB</Exponent>
   <P>3LJ5y4Vla7cS3XgmbIH5dQgppUHa+aSWavEOCbDRS/M....</P>
   <Q>1QyGIAnjv4YLcRVdwXtxWkijc+aZ496qIBZnCAUUD/E....</Q>
   <DP>0821dc0f+LBKOqIEvj4+2kJrNV5ueQesFBYkEsjPFM....</DP>
   <DQ>ugSzX2oDJwjdGKG1OOiVcmUWAm6IU4PpOxcUYtY8TC....</DQ>
   <InverseQ>LDQIQu+LSB6CSZBrGxNQthWi9mkuPGVZyDDr....</InverseQ>
   <D>qZm2bXKH8WwbsJ8ZlT3S1TbgUifppLrqSRkb8XqEcMv....</D> 
</RSAKeyValue>

The generated public key should be used in other apps (PHP / JavaScript / JAVA) to encrypt data. What part of the above XML defines the public key / what part do I have to send to the developers of the other apps?

And on the opposite side: What defines the private key / which part/parts do I have to store to be able to decrypt the data encrypted by my public key?

Exponent and modulus define the public key.

If you use RSA.ToXmlString with its sole parameter includePrivateParameters set to false , you will only see the format

<RSAKeyValue>
   <Modulus>…</Modulus>
   <Exponent>…</Exponent>
</RSAKeyValue>

output.

RSA keys for other parties are complicated to generate in .NET. If you send them this XML, not all your partners will be able to use it. General format for the public key is something like that:

在此处输入图像描述

The xml you've proived is a private key. To generate public key you should use the

string publicPrivateKey = RSA.ToXmlString(false);

Result will be like that:

<RSAKeyValue>
   <Modulus>t6tLd1Wi7PEkwPfx9KGP1Ps/5F2saXnOsCE2U....</Modulus>
   <Exponent>AQAB</Exponent>
</RSAKeyValue>

You may want to use other parties to get key in other format:

http://www.codeproject.com/KB/security/RSACryptoPad.aspx

To port Java key to .NET format you can use this project:

http://www.codeproject.com/KB/security/porting_java_public_key.aspx

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