Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Security.Cryptography;
Using System.IO;
Namespace TRSLMS.Website.Common
{
<summary>
MD5 encryption
</summary>
public class Encrypt
{
private static MD5 MD5 = MD5. Create ();
Privatization constructors
Private Encrypt () {}
<summary>
Hashing a string using UTF8 encoding
</summary>
<param name= "sourcestring" > Encrypted string </param>
<returns> return encrypted string non-solvable </returns>
public static string SetEncryptMD5 (String sourcestring)
{
Try
{
byte[] Source = Md5.computehash (Encoding.UTF8.GetBytes (sourcestring));
StringBuilder Sbuilder = new StringBuilder ();
for (int i = 0; i < source. Length; i++)
{
Sbuilder.append (Source[i]. ToString ("X2"));
}
return sbuilder.tostring ();
}
catch (Exception)
{
Return "";
}
}
//**********************************************************************
Note that it is 8 characters, 64 bits
Const string key_64 = "Loubyuan";
Const string iv_64 = "19920923";
<summary>
Des encryption method
</summary>
<param name= "sourcestring" > Encrypted string </param>
<returns> post-encryption string </returns>
public static string Setencryptdes (String sourcestring)
{
Try
{
byte[] Bykey = System.Text.ASCIIEncoding.ASCII.GetBytes (key_64);
byte[] Byiv = System.Text.ASCIIEncoding.ASCII.GetBytes (iv_64);
DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider ();
int i = cryptoprovider.keysize;
MemoryStream ms = new MemoryStream ();
CryptoStream CST = new CryptoStream (MS, Cryptoprovider.createencryptor (Bykey, Byiv), cryptostreammode.write);
StreamWriter SW = new StreamWriter (CST);
Sw. Write (sourcestring);
Sw. Flush ();
Cst. FlushFinalBlock ();
Sw. Flush ();
Return convert.tobase64string (Ms. GetBuffer (), 0, (int) Ms. Length);
}
Catch {return "";}
}
<summary>
Des decryption
</summary>
<param name= "sourcestring" > Decrypt string </param>
<returns> post-decryption string </returns>
public static string Getdecryptdes (String sourcestring)
{
Try
{
byte[] Bykey = System.Text.ASCIIEncoding.ASCII.GetBytes (key_64);
byte[] Byiv = System.Text.ASCIIEncoding.ASCII.GetBytes (iv_64);
byte[] Byenc = convert.frombase64string (sourcestring);
DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider ();
MemoryStream ms = new MemoryStream (BYENC);
CryptoStream CST = new CryptoStream (MS, Cryptoprovider.createdecryptor (Bykey, Byiv), cryptostreammode.read);
StreamReader sr = new StreamReader (CST);
return Sr. ReadToEnd ();
}
Catch {return "";}
}
//**********************************************************************
Static byte[] Keyarray = UTF8Encoding.UTF8.GetBytes ("be4l4d5d0ff2djm2fpqmvy8flahbde4h");
//<summary>
//AES encryption Method
//</summary>
//<param name= "sourcestring" > Encrypted string </ Param>
//<returns> string </returns> after encryption,
public static string Setencryptaes (String sourcestring)
{
Try
{
byte[] Toencryptarray = UTF8Encoding.UTF8.GetBytes (sourcestring);
RijndaelManaged Rdel = new RijndaelManaged ();
Rdel.key = Keyarray;
Rdel.mode = CIPHERMODE.ECB;
rdel.padding = PADDINGMODE.PKCS7;
ICryptoTransform ctransform = Rdel.createencryptor ();
byte[] resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);
Return convert.tobase64string (resultarray, 0, resultarray.length);
}
Catch {return "";}
}
<summary>
AES Decryption
</summary>
<param name= "sourcestring" > Decrypt string </param>
<returns> post-decryption string </returns>
public static string Getdecryptaes (String sourcestring)
{
Try
{
byte[] Toencryptarray = convert.frombase64string (sourcestring);
RijndaelManaged Rdel = new RijndaelManaged ();
Rdel.key = Keyarray;
Rdel.mode = CIPHERMODE.ECB;
rdel.padding = PADDINGMODE.PKCS7;
ICryptoTransform ctransform = Rdel.createdecryptor ();
byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);
Return UTF8Encoding.UTF8.GetString (resultarray);
}
Catch {return "";}
}
}
}
C # Common encryption methods