C # encryption-RSA

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.