具体转载哪一位网友的已经 不知道 了。在此谢谢这位网友
/// <summary>
/// 加密文本/// </summary>
public class Encryption
{
public static string ShadowPassword(string loginpassword, String strKey)
{
//加密
//loginpassword 需要加密的文本
//strKey 加密时使用的关键字,俗称密码,这个值必须是8位。
//返回值 加密后的文本
try
{
DESCryptoServiceProvider Des = new DESCryptoServiceProvider();
Byte[] inputByteArray = null;
inputByteArray = Encoding.Default.GetBytes(loginpassword);
Des.Key = ASCIIEncoding.ASCII.GetBytes((MD5EncryptProc(strKey).Substring(8)).PadRight(8));
Des.IV = ASCIIEncoding.ASCII.GetBytes((MD5EncryptProc(strKey).Substring(8)).PadRight(8));
System.IO.MemoryStream MS = new System.IO.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();
}
catch (Exception ex)
{
return ex.Message;
}
}
public static string backpassword(String password, String strKey)
{
//解密
//password 需要解密的文本
//strKey 解密时使用的关键字,必需和加密时的关健字相同,才可以解密出正确的文本,这个值必须是8位
//返回值 解密后的文本
try
{
DESCryptoServiceProvider Des = new DESCryptoServiceProvider();
int intLen;
intLen = password.Length / 2;
Byte[] inputByteArray = new Byte[intLen];
int i;
for (int x = 0; x < intLen; x++)
{
i = Convert.ToInt32(password.Substring(x * 2, 2), 16);
inputByteArray[x] = Convert.ToByte(i);
}
Des.Key = ASCIIEncoding.ASCII.GetBytes((MD5EncryptProc(strKey).Substring(8)).PadRight(8));
Des.IV = ASCIIEncoding.ASCII.GetBytes((MD5EncryptProc(strKey).Substring(8)).PadRight(8));
System.IO.MemoryStream MS = new System.IO.MemoryStream();
CryptoStream CS = new CryptoStream(MS, Des.CreateDecryptor(), CryptoStreamMode.Write);
CS.Write(inputByteArray, 0, inputByteArray.Length);
CS.FlushFinalBlock();
return Encoding.Default.GetString(MS.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}
private static string MD5EncryptProc(String strData)
{
//用MD5加密方式加密文本,可以直接使用。在模块中的主要作用是用于加密关键字
//strData 需要加密的文本
//返回值 加密后的文本
System.Security.Cryptography.MD5CryptoServiceProvider MD = new MD5CryptoServiceProvider();
try
{
return System.Text.Encoding.Default.GetString(MD.ComputeHash(System.Text.Encoding.Default.GetBytes(strData.Trim())));
}
catch (Exception ex)
{
return ex.Message;
}
}
}