Implementation of RSA encryption in ASP. NET

Source: Internet
Author: User
Tags modulus asymmetric encryption
In our practical use, encryption is an important means to ensure data security. In the past, MD5 and sha1 can be used for data encryption when ASP was used. Algorithm These two algorithms are fast and effective, but they cannot 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 the applicable encryption and decryption Program However, this requires a high level of mathematics for the writers, and it is difficult for ordinary people to do so.

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 by 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.

This section describes the principles of the RSA algorithm.ArticleOr there are many books. You can refer to them and won't 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:

Reference content is as follows:
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. See the followingCodeAs shown in, we can write a function to complete the encryption process:

Reference content is as follows:
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:

Reference content is as follows:
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:

The reference content is as follows:
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 ".

original address: http://www.evget.com/zh-CN/Info/ReadInfo.aspx? Id = 9167

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.