Nagaraj Tantri
Nagaraj Tantri

Reputation: 5262

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.

Upvotes: 3

Views: 5190

Answers (3)

CodesInChaos
CodesInChaos

Reputation: 108850

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>

Upvotes: 6

Random Dev
Random Dev

Reputation: 52290

Hmm, I guess my comment is a good answer after all:

As you only transformed the modulus part of the keys and this part is there in both privat and public key it's no wonder.

See here: http://en.wikipedia.org/wiki/RSA_%28algorithm%29#Key_generation

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503140

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.

Upvotes: 2

Related Questions