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