Encryption of common Asp. Net tools-asymmetric encryption RSA algorithm, asp. netrsa

Source: Internet
Author: User
Tags asymmetric encryption

Encryption of common Asp. Net tools-asymmetric encryption RSA algorithm, asp. netrsa

    

 

 

After several years of entering the programmer industry, there have been gains (enhanced technology) and efforts (time and sub-health status) in the past few years ). Of course, there is no regret, and the code is still long !!!

 

On The Way, never stop !!!

 

In the development process, I have also accumulated some experience, code blocks, and help classes. With these, I have indeed facilitated subsequent development processes and reduced the development cycle, for details, see the figure (a small part). These are all recently written in a row in combination with common development methods, and related projects are also used. Now it looks a little excited!

    Of course, sorry, I am a newbie. I need a lot of advice from the elders in the garden. Especially in terms of code Quality and Technology, you are very grateful and I am moving forward!

 

Self-help class

I hope you can give me a lecture and analysis in different cycles !!!

 

Let's start with encryption and decryption today, haha.

 

In the development process, various encryption methods emerge one after another, including symmetric encryption and asymmetric encryption. Of course, tokens are also classified into reversible and irreversible.

 

In c #, asymmetric encryption is represented by the RSA algorithm. encryption and decryption are performed using the public key and private key.

 

You must set the key length during encryption to ensure that the key length is feasible. Key Length: 8192.

 

The RSA encryption algorithm process is as follows:

 

1. First, the [system] generates a pair of keys, namely, the private key and public key.

 

2. Then, [system] sends the public key to [user]

 

3. [user] encrypts the data with the received public key and sends it to [system]

 

4. After receiving the data, the system decrypts the data using its own private key and returns the password.

 

In asymmetric algorithms, the data encrypted by the public key must be decrypted using the corresponding private key, and the private key is only known to the receiver. This ensures the security of data transmission.

 

The theory is strong. Here is a DEMO of the Code:

 

Public static class RSA {// <summary> // generate a key // <param name = "PrivateKey"> Private Key </param> /// <param name = "PublicKey"> Public Key </param> // <param name = "KeySize"> key length: 8192, 2048, </param> // </summary> public static void Generator (out string PrivateKey, out string PublicKey, int KeySize =) {RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (KeySize); PrivateKey = rsa. toXmlString (true );// If the parameter "true" is used to export the private key of the RSA algorithm to the string PrivateKey, true indicates that the exported private key is both RSA public key and private key. false indicates that only the public key is included. PublicKey = rsa. toXmlString (false); // exports the RSA algorithm Public Key to the string PublicKey. If the parameter is false, the private key is not exported. If the parameter is true, the RSA public key and the private key are both included; false indicates that only the public key is included .} /// <Summary> // RSA encryption imports the public key to the RSA object, prepare the encrypted string /// </summary> /// <param name = "PublicKey"> Public Key </param> /// <param name = "encryptstring"> </param> public static string RSAEncrypt (string PublicKey, string encryptstring) {byte [] PlainTextBArray; byte [] CypherTextBArray; string Result; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (); rsa. fromXmlString (PublicKey); PlainTextBArray = (new UnicodeEncoding ()). getBytes (encryptstring); CypherTextBArray = rsa. encrypt (PlainTextBArray, false); Result = Convert. toBase64String (CypherTextBArray); return Result;} // <summary> // RSA decrypts and imports the private key to RSA, prepare for decryption /// </summary> /// <param name = "PrivateKey"> Private Key </param> /// <param name = "decryptstring"> to be decrypted string </param> public static string RSADecrypt (string PrivateKey, string decryptstring) {byte [] PlainTextBArray; byte [] DypherTextBArray; string Result; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (); rsa. fromXmlString (PrivateKey); PlainTextBArray = Convert. fromBase64String (decryptstring); DypherTextBArray = rsa. decrypt (PlainTextBArray, false); Result = (new UnicodeEncoding ()). getString (DypherTextBArray); return Result ;}} call method: static void Main (string [] args) {string PrivateKey = ""; string PublicKey = ""; RSA. generator (out PrivateKey, out PublicKey, 1024); var aaa = RSA. RSAEncrypt (PublicKey, "123456789"); var bbb = RSA. RSADecrypt (PrivateKey, aaa );}

 

OK, you can see that this issue of asymmetric encryption is written here. Thank you for your support. Your support is my motivation!

In the next phase, we will introduce several common symmetric encryption methods !!!

 

Personal headlines:Http://www.toutiao.com/c/user/3213034222/#mid=4129397771

 

Related Article

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.