public static string MD5Encrypt(string pToEncrypt)//加密
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定義偏移量 (可自定義)
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定義密鑰(可自定義)
des.Key = key;
des.IV = iv;
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);
}
return ret.ToString();
byte[] bytResult = ms.ToArray();
string strResult = BitConverter.ToString(bytResult);
strResult = strResult.Replace("-", "");
return strResult;
}
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;
}
byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定義偏移量(可自定義對應解密)
byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定義密鑰(可自定義對應解密)
des.Key = key;
des.IV = iv;
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.Default.GetString(ms.ToArray());
}