开发者

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

开发者 https://www.devze.com 2023-03-18 07:00 出处:网络
I\'ve generated a new public/private key pair and exported it as an XML string: RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048);

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:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号