How to implement RSA encryption and ASP. netrsa encryption in asp. Net

Source: Internet
Author: User
Tags modulus asymmetric encryption

How to implement RSA encryption and ASP. netrsa encryption in asp. Net

In our practical use, encryption is an important means to ensure data security. In the past, MD5 and SHA1 algorithms were used for data encryption when ASP was used. Although these two algorithms are fast and effective, they cannot be used to reverse the ciphertext encrypted by them, that is, decryption. Therefore, these two methods are not suitable for data decryption. Of course, you can also write suitable encryption and decryption programs by yourself. However, this requires a high level of mathematics for the writers, which is hard for ordinary people.

Now, with the release of ASP. Net, the programming mode under ASP has been completely changed. We can use the encryption service provided by classes in. Net Framework to ensure data security. Currently, RSA is widely used for encryption. In. Net Framework, there are two main classes related to RSA encryption algorithms: RSA class and RSACryptoServiceProvider class. According to MSDN, the RSA class "represents the base class inherited from all the implementations of the RSA algorithm", and the RSACryptoServiceProvider class is "using the encryption service provider (CSP) the provided RSA algorithm implements asymmetric encryption and decryption ". In addition, the RSAParameters structure of "representing standard parameters of the RSA algorithm" is also very important. It stores the parameters of the RSA algorithm.

As there are many articles or books about the principles of RSA Algorithms, you can refer to them and will not repeat them here. The following describes how to implement RSA encryption in ASP. Net.

Generation of RSA parameters: the RSA parameter type is the RSAParameters structure mentioned above, according to MSDN, it contains eight fields: D, DP, DQ, Exponent, InverseQ, Modulus, P, and Q. Only Exponent and Modulus values are required for encryption, which can be considered as public keys. All fields are required for decryption and can be considered as private keys. The following program shows how to generate two RSA parameters:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ();
RSAParameters rsaParamsExcludePrivate = rsa. ExportParameters (false );
RSAParameters rsaParamsIncludePrivate = rsa. ExportParameters (true );

The ExportParameters (bool) method of the RSACryptoServiceProvider class is used to export the RSA parameter. true indicates to export the "Private Key" of the preceding eight fields, and false indicates to export the "Public Key ".

Encryption and decryption using the RSA parameter: In this step, you need to import the above two parameters to the RSACryptoServiceProvider class object, and then use it to encrypt the data. As shown in the following code, we can write a function to complete the encryption process:

Public byte [] RSAEncrypt (byte [] B)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ();
Rsa. ImportParameters (rsaParamsExcludePrivate); // import the Public Key
Byte [] EncryptedData = rsa. Encrypt (DataToEncrypt, false );
Return EncryptedData;
}

During decryption, you only need to replace rsa. ImportParameters (rsaParamsExcludePrivate) with rsa. ImportParameters (rsaParamsExcludePrivate), and then change Encrypt to Decrypt.

Save and load RSA parameters: the RSA parameters can be saved in XML format. The following code shows how to save and load them (only key parts are listed)

  Save:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ();
StreamWriter writer = new StreamWriter (@ "d: \ PublicAndPrivateKey. xml ");
String PPKeyXml = rsa. ToXmlString (true); // Save the Private Key
Writer. Write (PPKeyXml );
Writer. Close ();
Writer = new StreamWriter (@ "d: \ PublicKey. xml ");
String PKeyXml = rsa. ToXmlString (false); // Save the Public Key
Writer. Write (PKeyXml );
Writer. Close ();

  Read:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ();
StreamReader reader = new StreamReader (@ "d: \ PublicKey. xml ");
String PKey = reader. ReadToEnd ();
Rsa. FromXmlString (PKey );
Reader. Close ();
StreamReader reader = new StreamReader (@ "d: \ PublicAndPrivateKey. xml ");
String PPKey = reader. ReadToEnd ();
Reader. Close ();

The ToXmlString and ExportParameters methods are similar. false indicates that the "Public Key" is saved, and true indicates that the "Private Key" is saved ".

The above is the main method to implement RSA encryption in ASP. Net.

Reprinted from: http://www.aspnetjia.com

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.