如何使用公钥获取RSA私钥? [英] How do I get the RSA Private Key goes with the public key?

查看:134
本文介绍了如何使用公钥获取RSA私钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#示例使用DOTNETUTLIITES生成RSA公钥,格式化密钥并将其传递给XML字符串。



我还有另外一个C#示例使用RSA生成RSA公钥和私钥对,但不格式化公钥。



如何使用第二个示例中的技术来生成密钥对和从第一个示例获取公钥的格式。



第一个例子:

  var  publicKey = DotNetUtilities.GetRsaPublicKey(cryptoProvider); 
var publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
var publicEncodedBytes = publicKeyInfo.ToAsn1Object()。GetDerEncoded();
var publicEncodedString = Convert.ToBase64String(publicEncodedBytes);



Secone示例:

 RSACryptoServiceProvider rsa =  new  RSACryptoServiceProvider(); 
string privateKey = rsa.ToXmlString( true );
File.WriteAllText(Application.StartupPath + \\PrivateKey.xml,专用密钥);
string publicKey = rsa.ToXmlString( false );
File.WriteAllText(Application.StartupPath + \\PublicKey.xml,公钥);
MessageBox.Show( 密钥对在以下位置成功创建:\ n + Application .StartupPath);

解决方案

这个问题毫无意义,关于来自第一个例子。首先,加密不可行只得到一个密钥并找出第二个密钥。如果有可能,谁会想要使用这种加密?在第一个代码示例中,您只获取公钥。如果这是在返回的方法中完成的,并且访问 cryptoProvider ,那就亲吻它吧:除了公钥之外什么都没有,这完全没用。您可以加密某些数据而无法对其进行解密,或者您可以拥有一个可以验证的签名文档,但永远无法生成具有相同提供程序的第二个文档,这会破坏所有内容的目的。



也许你错过了公钥加密的想法,所以试着理解它: http://en.wikipedia.org/wiki/Public-key_cryptography [ ^ ]。







另请参阅我过去的答案,我试图解释公钥加密如何在安全架构中工作:如何确保只有一个定义良好的客户端与Web服务(WCF)通信? [ ^ ]。



[结束编辑]



第二个例子有意义,但你理解错误: rsa.ToXmlString(true)不会给你公钥;它给你们两个;这个布尔参数是: includePrivateParameters

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa.toxmlstring%28v=vs。 110%29.aspx [ ^ ]。



它解决了您的问题。

< br $> b $ b



请不要重新发布你的问题。如果您遇到一些问题,请在此页上询问您的后续问题。



-SA

I have a C# example uses DOTNETUTLIITES to produce an RSA public key, formats the key and passes it in an XML string.

I have another C# examples that produces an RSA Public and Private key pair using RSA but does not format the public key.

How can I use the technic in the second example to produce the key pair and get the formatting of the public key from the first example.

First Example:

var publicKey = DotNetUtilities.GetRsaPublicKey(cryptoProvider);
        var publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
        var publicEncodedBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
        var publicEncodedString = Convert.ToBase64String(publicEncodedBytes);


Secone Example:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            string privateKey = rsa.ToXmlString(true);
            File.WriteAllText(Application.StartupPath + "\\PrivateKey.xml", privateKey);
            string publicKey = rsa.ToXmlString(false);
            File.WriteAllText(Application.StartupPath + "\\PublicKey.xml", publicKey);
            MessageBox.Show("The Key pair created successfully at:\n" + Application.StartupPath);

解决方案

The question makes no sense, about "from the first example". First of all, it is cryptographically infeasible to get have only one key and figure out the second key. If it was possible, who would ever want to use such "encryption"? In first code sample, you only get the public key. If this is done in the method which returns, and the access to the cryptoProvider, kiss it bye-bye: you got nothing except the public key, which is totally useless. You can encrypt some data without a possibility to decrypt it, or you could have a signed document which you could validate, but never be able to generate a second document with the same provider, which would defeat the purpose of everything.

Perhaps you are missing the idea of the public-key cryptography, so try to understand it: http://en.wikipedia.org/wiki/Public-key_cryptography[^].

[EDIT]

Please also see my past answer where I tried to explain how public-key cryptography works in a security schema: How to ensure that only a well defined client talks to a Web service (WCF)?[^].

[END EDIT]

The second example makes sense, but you understand it incorrectly: rsa.ToXmlString(true) does not give you public key; it give you both; this Boolean parameter is: includePrivateParameters:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa.toxmlstring%28v=vs.110%29.aspx[^].

It solves your problem.

[EDIT]

Please don't re-post your question. If you have some problems, ask your follow-up questions on this page.

—SA


这篇关于如何使用公钥获取RSA私钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆