In our practical application, encryption is an important means to ensure data security. Before using ASP, data encryption can use the MD5 and SHA1 algorithms, these two algorithms, although fast and efficient, but can not be encrypted through their ciphertext to reverse operation, that is, decryption. So when you need to decrypt the data, these two methods don't fit. Of course, you can also write the appropriate encryption and decryption procedures, but this to the writer's mathematical level is very high requirements, the average person is difficult to do.
Now, with the introduction of ASP.net, completely changed the previous ASP's programming mode. We are able to leverage the cryptographic services provided by classes in the. Net framework to ensure data security. At present, the most widely used encryption method is to use RSA algorithm to encrypt. There are two classes in the. Net framework that are related to RSA encryption algorithms: The RSA class and the RSACryptoServiceProvider class. According to MSDN, the RSA class is the "base class from which all implementations of the RSA algorithm inherit", while the RSACryptoServiceProvider class is "performing asymmetric encryption and decryption using the implementation of the RSA algorithm provided by the cryptographic service provider (CSP)." In addition, the RSAParameters structure that represents the standard parameters for the RSA algorithm is also important, and it holds the parameters of the RSA algorithm.
Because the introduction of RSA algorithm principle of the article or book more, we can refer to, this is no longer mentioned. The following highlights how to implement RSA encryption in ASP.net.
RSA parameter generation: The type of the RSA parameter is the RSAParameters structure mentioned above, and review MSDN to see that it contains D, DP, DQ, Exponent, Inverseq, modulus, P, Q eight fields. Encryption requires only the exponent and modulus two values, and can be viewed as a public key. All fields are required for decryption and can be considered as private keys. The following program shows how to generate RSA two parameters:
Copy Code code 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 to export the "private key" of the eight fields above, and False indicates that the "public key" is exported.
Encrypt decryption using RSA parameters: This step requires importing the two parameters above into the RSACryptoServiceProvider class object and then encrypting the data. As shown in the following code, we can write a function to complete the encryption process:
Copy Code code as follows:
public byte [] Rsaencrypt (Byte [] b)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ();
Rsa. ImportParameters (rsaparamsexcludeprivate); Import Public key
byte [] Encrypteddata=rsa. Encrypt (Datatoencrypt,false);
return EncryptedData;
}
When decrypting, just put RSA. ImportParameters (rsaparamsexcludeprivate) is replaced by RSA. ImportParameters (Rsaparamsexcludeprivate), and then replace the encrypt decrypt on the line.
Save and load RSA parameters: RSA parameters can be saved in XML format, and the following code shows how to save and load (only the key parts are listed)
Save:
Copy Code code as follows:
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ();
StreamWriter Writer=new StreamWriter (@ "D:\PublicAndPrivateKey.xml");
String Ppkeyxml=rsa. Toxmlstring (TRUE);//Save private key writer. Write (Ppkeyxml);
Writer. Close ();
Writer=new StreamWriter (@ "D:\PublicKey.xml");
String Pkeyxml=rsa. Toxmlstring (false);//Save public key
Writer. Write (Pkeyxml);
Writer. Close ();
Read:
Copy Code code 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 ();
Toxmlstring and Exportparameters methods are similar, false indicates that the "public key" is saved, and true indicates that the "private key" is saved.