Click here to Skip to main content
16,002,104 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
var publicKey = DotNetUtilities.GetRsaPublicKey(cryptoProvider);
        var publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
        var publicEncodedBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
        var publicEncodedString = Convert.ToBase64String(publicEncodedBytes);

Secone Example:
C#
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);
Posted

1 solution

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900