using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Web.Security; namespace _1200Gxml心电图绘制 { public class DesEncrypt { #region ========加密======== /// /// 加密 /// /// /// public static string Encrypt(string text) { return Encrypt(text, "da1ff5@1b453d1f66d83bc6e7c5cab4$"); } /// /// 加密数据 /// /// /// /// public static string Encrypt(string text, string sKey) { var des = new DESCryptoServiceProvider(); var inputByteArray = Encoding.Default.GetBytes(text); var hashPasswordForStoringInConfigFile = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"); if (hashPasswordForStoringInConfigFile != null) des.Key = Encoding.ASCII.GetBytes(hashPasswordForStoringInConfigFile.Substring(0, 8)); var passwordForStoringInConfigFile = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"); if (passwordForStoringInConfigFile != null) des.IV = Encoding.ASCII.GetBytes(passwordForStoringInConfigFile.Substring(0, 8)); var ms = new MemoryStream(); var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); var ret = new StringBuilder(); foreach (var b in ms.ToArray()) ret.AppendFormat("{0:X2}", b); return ret.ToString(); } #endregion #region ========解密======== /// /// 解密 /// /// /// public static string Decrypt(string text) { return Decrypt(text, "da1ff5@1b453d1f66d83bc6e7c5cab4$"); } /// /// 解密数据 /// /// /// /// public static string Decrypt(string text, string sKey) { var des = new DESCryptoServiceProvider(); var len = text.Length / 2; var inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(text.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } var hashPasswordForStoringInConfigFile = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"); if (hashPasswordForStoringInConfigFile != null) des.Key = Encoding.ASCII.GetBytes(hashPasswordForStoringInConfigFile.Substring(0, 8)); var passwordForStoringInConfigFile = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5"); if (passwordForStoringInConfigFile != null) des.IV = Encoding.ASCII.GetBytes(passwordForStoringInConfigFile.Substring(0, 8)); var ms = new MemoryStream(); var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } #endregion } }