The encryption and decryption of ASP (C #):
1. Import the required packages:
Using System.IO;
Using System.Text;
Using System.Security.Cryptography;
2. Encryption
1) MD5 General encryption
Get the field to encrypt and convert to byte[] Array
byte[] data = System.Text.Encoding.Unicode. GetBytes (str. ToCharArray ());
Establishing Cryptographic Services
System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider ();
Encrypt byte[] Array
Byte[] result = Md5.computehash (data);
Response.Write ("MD5 normal encryption:" + System.Text.Encoding.Unicode.GetString (result));
2) MD5 password encryption [common]
Response.Write ("MD5 Password encryption:" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5" ));
3) ASP. How to encrypt and decrypt querystring in net [frequently used]
Encryption
Response.Redirect ("detailinfo.aspx?id=" + convert.tobase64string (System.Text.Encoding.Default.GetBytes (str)). Replace ("+", "%2b"));
Decrypt
String id = System.Text.Encoding.Default.GetString (convert.frombase64string (request.querystring["id"). ToString (). Replace ("%2b", "+"));
Encryption and decryption functions:
Protected Function Encode (ByVal thestr As String) as String
Return convert.tobase64string (System.Text.Encoding.Default.GetBytes (THESTR)). Replace ("+", "%2b")
End Function
Protected Function Decode (ByVal thestr As String) as String
Return System.Text.Encoding.Default.GetString (convert.frombase64string (Thestr.replace ("%2b", "+"))
End Function
4) DES encryption and decryption algorithm [common key algorithm]
public static string key = "DKMAB5DE";//encryption key must be 8 bits
Encryption algorithm
public static string Md5encrypt (String ptoencrypt)
{DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
byte[] Inputbytearray = Encoding.Default.GetBytes (Ptoencrypt);
Des. Key = ASCIIEncoding.ASCII.GetBytes (key);
DES.IV = ASCIIEncoding.ASCII.GetBytes (Key);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
StringBuilder ret = new StringBuilder ();
foreach (Byte b in Ms. ToArray ()) {
Ret. AppendFormat ("{0:x2}", b);
}
Ret. ToString ();
return ret. ToString ();
}
Decryption algorithm
public static string Md5decrypt (String ptodecrypt)
{DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
byte[] Inputbytearray = new BYTE[PTODECRYPT.LENGTH/2];
for (int x = 0; x < PTODECRYPT.LENGTH/2; × x + +) {
int i = (Convert.ToInt32 (ptodecrypt.substring (x * 2, 2), 16));
INPUTBYTEARRAY[X] = (byte) i;
}
Des. Key = ASCIIEncoding.ASCII.GetBytes (key);
DES.IV = ASCIIEncoding.ASCII.GetBytes (Key);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
StringBuilder ret = new StringBuilder ();
Return System.Text.Encoding.ASCII.GetString (Ms. ToArray ());
}
5) RSA encryption and decryption algorithm [common key algorithm]
Encryption algorithm
public string Rsaencrypt (string encryptstring)
{CspParameters CSP = new CspParameters ();
Csp. KeyContainerName = "Whaben";
RSACryptoServiceProvider Rsaprovider = new RSACryptoServiceProvider (CSP);
byte[] encryptbytes = Rsaprovider.encrypt (ASCIIEncoding.ASCII.GetBytes (encryptstring), true);
String str = "";
foreach (Byte b in encryptbytes)
{str = str + string. Format ("{0:x2}", b);
}
return str;
}
Decryption algorithm
public string Rsadecrypt (string decryptstring)
{CspParameters CSP = new CspParameters ();
Csp. KeyContainerName = "Whaben";
RSACryptoServiceProvider Rsaprovider = new RSACryptoServiceProvider (CSP);
int length = (DECRYPTSTRING.LENGTH/2);
byte[] decryptbytes = new Byte[length];
for (int index = 0; index < length; index++)
{String substring = decryptstring.substring (Index * 2, 2);
Decryptbytes[index] = convert.tobyte (substring, 16);
}
Decryptbytes = Rsaprovider.decrypt (Decryptbytes, true);
Return ASCIIEncoding.ASCII.GetString (decryptbytes);
}