In actual application of RSA, the receiver generates a public key and a private key, and the sender encrypts it with the public key, and then sends the encrypted content to the receiver.
The namespace of CspParameters is:
System.Security.Cryptography
CspParameters cpSend = new CspParameters (); // Csp = Cryptography Service ProviderCspParameters cpReceive = new CspParameters (); cpSend. keyContainerName = "SendTestContainer"; cpReceive. keyContainerName = "etetestcontainer"; RSACryptoServiceProvider rsaSend = new RSACryptoServiceProvider (cpSend); RSACryptoServiceProvider rsaReceive = new RSACryptoServiceProvider (cpReceive); rsaSend. fromXmlString (rs AReceive. toXmlString (false); // The sender uses the receiver's public key to encrypt it. string plaintext = "a few days ago, I met a friend who asked me to fight with the thief and I immediately agreed to him, I think this is boring. "; Byte [] ciphertext = rsaSend. encrypt (System. text. encoding. UTF8.GetBytes (plaintext), false); // encrypted byte [] decryption = rsaReceive. decrypt (ciphertext, false); // After decryption, lbl. width = 760; lbl. text = ""; lbl. text + = Convert. toBase64String (ciphertext) + "<br/>"; // display the encrypted lbl. text + = System. text. encoding. UTF8.GetString (decryption) + "<br/>"; // display the decrypted lbl. text + = Server. htmlEncode (rsaSend. toXmlString (false) + "<br/>"; // display the sender's public key lbl. text + = Server. htmlEncode (rsaReceive. toXmlString (true) + "<br/>"; // display the receiver's public key and private key lbl. text + = Server. htmlEncode (rsaReceive. toXmlString (false) + "<br/>"; // displays the Public Key rsaSend of the receiver. persistKeyInCsp = true; // The key must be saved. // rsaSend. clear (); rsaReceive. persistKeyInCsp = true; // rsaReceive. clear ();
In the above Code, we use CspParameters to save the key. ToXmlString and FromXmlString tell the sender of the receiver's public key.