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