using System; using System.IO; using System.Text; using System.Security.Cryptography; /// <summary> /// Encrypt_D 的摘要说明 /// </summary> public class SysEncrypt { /// <summary> /// 使用MD5进行数据加密。str:需要加密的数据, kind: 加密类型:1-普通加密;2-密码加密, returns:返回加密的数据 /// </summary> public string GetMD5(string str, int kind) { string reStr = ""; //获取要加密的字段,并转化为Byte[]数组 byte[] data = System.Text.Encoding.Unicode.GetBytes(str.ToCharArray()); //建立加密服务 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); //1代表加密Byte[]数组 if (kind == 1) { //加密Byte[]数组 byte[] result = md5.ComputeHash(data); //将加密后的数组转化为字段 string sResult = System.Text.Encoding.Unicode.GetString(result); //MD5普通加密 reStr = sResult.ToString(); } else if (kind == 2)//2代表作为密码方式加密 { //作为密码方式加密 string EnPswdStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5"); //MD5密码加密 reStr = EnPswdStr; } return reStr.ToString(); } /// <summary> /// 对数据进行加密。 strText:传入加密的数据, return:返回加密后的字字符串 /// </summary> public string EncryptText(String strText) { return Encrypt(strText, "&%#@?,:*_"); } /// <summary> /// 对数据进行解密。strText:传入解密的数据, return:返回解密后的字字符串 /// </summary> public String DecryptText(String strText) { return Decrypt(strText, "&%#@?,:*_"); } /// <summary> /// 加密函数 /// </summary> private String Encrypt(String strText, String strEncrKey) { Byte[] byKey = { }; Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; try { byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (Exception ex) { return ex.Message; } } /// <summary> /// 解密函数 /// </summary> private String Decrypt(String strText, String sDecrKey) { Byte[] byKey = { }; Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; Byte[] inputByteArray = new byte[strText.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch (Exception ex) { return ex.Message; } } }