MD5 encryption
<summary>
MD5 encryption
</summary>
<param name= "Tocrystring" > string to encrypt </param>
<returns></returns>
public static string Getmd5str (String tocrystring)
{
MD5CryptoServiceProvider hashmd5;
HASHMD5 = new MD5CryptoServiceProvider ();
Return bitconverter.tostring (Hashmd5.computehash (Encoding.Default.GetBytes (tocrystring)). Replace ("-", ""). ToLower ();//asp is lowercase, lowercase all characters
}
DES encryption and decryption
///
DEC Encryption Process
///
public static string Encrypt (String ptoencrypt, String SKey)
{
Descryptoserviceproviderdes = new DESCryptoServiceProvider (); Put the string in a byte array
byte[] Inputbytearray = Encoding.Default.GetBytes (Ptoencrypt);
Byte[] Inputbytearray=encoding.unicode.getbytes (ptoencrypt);
Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey); Establish keys and offsets for encrypted objects
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey); GetBytes method of using Asciiencoding.ascii method in original text
MemoryStream ms = new MemoryStream (); Make the input password must be entered in English text
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 ();
}
///
DEC decryption Process
///
public static string Decrypt (String ptodecrypt, String SKey)
{
Descryptoserviceproviderdes = 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 (SKey); Establishes the key and offset of the encrypted object, which is important and cannot be modified
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);
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 (); Create Stringbuild object, Createdecrypt use Stream object, must turn decrypted text into stream object
Return System.Text.Encoding.Default.GetString (Ms. ToArray ());
}
The skey is very important, defined as a string and then assigned equal to eight letters or numbers, note that must be 8
This is also very useful, for example, you want to enter the article page, the parameters passed in the aid=10000
Then encrypt the 10000.
Then decrypt it when accepted. This will effectively prevent SQL injection attacks!
. NET two kinds of encryption methods MD5 and Dec