Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSA in C# public key is same as private key?

I searched a lot online but stuck with doubts in RSA public key and private key cryptography.

When I checked MSDN site, I tried this

 RSACryptoServiceProvider rsaEncryptDecrypt = new RSACryptoServiceProvider();

 byte[] privateKeyByte = rsaEncryptDecrypt.ExportParameters(true).Modulus;
 byte[] publicKeyByte = rsaEncryptDecrypt.ExportParameters(false).Modulus;

 string privateKey = Convert.ToBase64String(privateKeyByte);
 string publicKey = Convert.ToBase64String(publicKeyByte);

The string public key and private key are Same!!! Is it correct? I mean how can the strings be same? Isn't suppose to be two different keys?

Please correct me if I am wrong. I am confused !

Thank you in advance!

UPDATE

I mistook the parameters,

But then: When I saw

https://stackoverflow.com/questions/6592990/simple-rsa-encryption-decryption-in-net#answer-6593054"

How can I get string value? because I have to store it in App.config and access it whenever I want. I mean I need to store the public and private keys both in App.config

UPDATE2

I am sorry, I just used ToXmlString property of RSACryptoServiceProvider's instance. Got the private key and public key.

like image 545
Nagaraj Tantri Avatar asked Dec 06 '25 02:12

Nagaraj Tantri


2 Answers

The Modulus is the same for both.

The public key consists of the encryption exponent e and the modulus n.

Traditionally the decryption key consists of the decryption exponent d and the same modulus n. For better performance, it often includes some more numbers, such as the prime factors p and q of n.

To better visualize what a public key includes try ToXmlString(false/true)

Public key ToXmlString(false):

<RSAKeyValue>
    <Modulus>4ZpwnuksQkLuqLczu5eJcS6aIFaPsTwGpS57/P9rviJWI7sweYZnE/eBVtPVKoanhgHxmcHyk4GbyvCnXKSzDw==</Modulus>
    <Exponent>AQAB</Exponent>
</RSAKeyValue>

Public Key+Private Key ToXmlString(true):

<RSAKeyValue>
     <Modulus>4ZpwnuksQkLuqLczu5eJcS6aIFaPsTwGpS57/P9rviJWI7sweYZnE/eBVtPVKoanhgHxmcHyk4GbyvCnXKSzDw==</Modulus>
     <Exponent>AQAB</Exponent>
     <P>8lLDYv+MEBUdp0eUY3an4mlU7ovmyV6f60RJoXOB9Hs=</P>
     <Q>7lYYef5/PvPOyrN0HGZPt/RWknfVd4c3Kc6WVEZICX0=</Q>
     <DP>UI3GufAthWMfmm4nG/Fj2dYeD7aeH66/BpyKxYr6VmU=</DP>
     <DQ>sBZkFx30nWo8in5zdtgQZfTcUXLAAIOiOf0sDC+w4XE=</DQ>
     <InverseQ>GBkNq0KZ4ERaEO/oVQoQDONw6ZHixNimR5IJ7cbzKXw=</InverseQ>
     <D>ErLyUrmQ6Y0SqvlEWHAe/DqYm8WQ82e+RUKtFDM3gvK9ygloqftx6rhn9XvM/ji1JnrDqiuepn5T3D3F+3GVQQ==</D>
</RSAKeyValue>
like image 129
CodesInChaos Avatar answered Dec 07 '25 14:12

CodesInChaos


Look at the documentation for RSAParameters - the public key is formed from {e, n} (Exponent and Modulus). The private key is formed from {d, n} (D and Modulus). so when you call ExportParameters(false) you would get the same Modulus as that's part of the public information - but you won't get a value for the D property.

like image 25
Jon Skeet Avatar answered Dec 07 '25 14:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!