In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
RSA how to achieve C# encryption, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
RSA C# encryption first let's understand what is RSA, it belongs to asymmetric encryption, its principle is to use a public key and a private key, the public key can be used for encryption, and the private key can be strictly kept secret for decryption, so we know that RSA is suitable for encryption with a small amount of data, such as encryption keys for symmetric encryption.
Implementing C # encryption with RSA
The namespace of RSACryptoServiceProvider is:
System.Security. CryptographyRSACryptoServiceProvider rsaSend = new RSACryptoServiceProvider (); string plaintext = "plaintext"; / / plaintext byte [] ciphertext = rsaSend.Encrypt (System.Text.Encoding.UTF8.GetBytes (plaintext), false); / / encrypted lbl.Text = Convert.ToBase64String (ciphertext); / / shows encrypted, Base64 encoding is used to display invisible characters.
When you create a RSACryptoServiceProvider instance using RSACryptoServiceProvider (), the key is automatically generated.
In the practical application of RSA, the receiver generates the public key and the private key, the sender encrypts it with its public key, and then sends the encrypted content to the receiver.
The namespace of CspParameters is:
System.Security.CryptographyCspParameters cpSend = new CspParameters (); / / Csp = Cryptography ServiceProvider CspParameters cpReceive = new CspParameters (); cpSend.KeyContainerName = "SendTestContainer"; cpReceive.KeyContainerName = "ReceiveTestContainer"; RSACryptoServiceProvider rsaSend = new RSACryptoServiceProvider (cpSend);; RSACryptoServiceProvider rsaReceive = new RSACryptoServiceProvider (cpReceive); rsaSend.FromXmlString (rsaReceive.ToXmlString (false)) / / the sender uses the public key given by the receiver to encrypt string plaintext = "the other day I met a friend who asked me to fight with the horse thief, and I immediately promised him because I thought it was so boring." ; byte [] ciphertext = rsaSend.Encrypt (System.Text.Encoding.UTF8.GetBytes (plaintext), false); / / encrypted byte [] decryption = rsaReceive.Decrypt (ciphertext, false); / / decrypted lbl.Width = 760; lbl.Text = ""; lbl.Text + = Convert.ToBase64String (ciphertext) + "< br / >"; / / display 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 and private keys lbl.Text + = Server.HtmlEncode (rsaReceive.ToXmlString (false)) + "< br / >"; / / display the receiver's public key rsaSend.PersistKeyInCsp = true / / key to be saved / / rsaSend.Clear (); rsaReceive.PersistKeyInCsp = true; / / rsaReceive.Clear ()
In the above code, we use CspParameters to save the key, and ToXmlString and FromXmlString tell the sender the receiver's public key.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.