简体   繁体   English

C#公钥中的RSA与私钥相同?

[英]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. 我在网上搜索了很多,但在RSA公钥和私钥加密方面存在疑虑。

When I checked MSDN site, I tried this 当我检查MSDN网站时,我尝试了这个

 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 UPDATE

I mistook the parameters, 我误认了参数,

But then: When I saw 但那时:我看到了

https://stackoverflow.com/questions/6592990/simple-rsa-encryption-decryption-in-net#answer-6593054" 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. 因为我必须将它存储在App.config中并随时访问它。 I mean I need to store the public and private keys both in App.config 我的意思是我需要在App.config中存储公钥和私钥

UPDATE2 UPDATE2

I am sorry, I just used ToXmlString property of RSACryptoServiceProvider's instance. 对不起,我刚刚使用了RSACryptoServiceProvider实例的ToXmlString属性。 Got the private key and public key. 获得了私钥和公钥。

The Modulus is the same for both. Modulus对于两者都是相同的。

The public key consists of the encryption exponent e and the modulus n . 公钥由加密指数e和模数n

Traditionally the decryption key consists of the decryption exponent d and the same modulus n . 传统上,解密密钥由解密指数d和相同的模数n For better performance, it often includes some more numbers, such as the prime factors p and q of n . 为了获得更好的性能,它通常包含更多数字,例如n的素因子pq

To better visualize what a public key includes try ToXmlString(false/true) 为了更好地可视化公钥包含的内容,请尝试ToXmlString(false/true)

Public key ToXmlString(false) : 公钥ToXmlString(false)

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

Public Key+Private Key ToXmlString(true) : 公钥+私钥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>

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. 因为你只改变了键的模数部分,而且这部分在privat和public key中都存在,这也就不足为奇了。

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

Look at the documentation for RSAParameters - the public key is formed from {e, n} ( Exponent and Modulus ). 查看RSAParameters的文档 - 公钥由{e, n}ExponentModulus )组成。 The private key is formed from {d, n} ( D and Modulus ). 私钥由{d, n}DModulus )组成。 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. 因此,当您调用ExportParameters(false)获得与公共信息相同的模数 - 但您不会获得D属性的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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