加密解密
1 public static string Encrypt(string Text, string sKey) 2 { 3 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 4 byte[] inputByteArray; 5 inputByteArray = Encoding.Default.GetBytes(Text); 6 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 7 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 8 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 9 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);10 cs.Write(inputByteArray, 0, inputByteArray.Length);11 cs.FlushFinalBlock();12 StringBuilder ret = new StringBuilder();13 foreach (byte b in ms.ToArray())14 {15 ret.AppendFormat("{0:X2}", b);16 }17 return ret.ToString();18 }19 20 public static string Decrypt(string Text, string sKey)21 {22 System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();23 int len;24 len = Text.Length / 2;25 byte[] inputByteArray = new byte[len];26 int x, i;27 for (x = 0; x < len; x++)28 {29 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);30 inputByteArray[x] = (byte)i;31 }32 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));33 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));34 System.IO.MemoryStream ms = new System.IO.MemoryStream();35 System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);36 cs.Write(inputByteArray, 0, inputByteArray.Length);37 cs.FlushFinalBlock();38 return Encoding.Default.GetString(ms.ToArray());39 }40 41 public static string BytesToHexString(byte[] input)42 {43 StringBuilder hexString = new StringBuilder(64);44 45 for (int i = 0; i < input.Length; i++)46 {47 hexString.Append(String.Format("{0:X2}", input[i]));48 }49 return hexString.ToString();50 }51 52 public static byte[] HexStringToBytes(string hex)53 {54 if (hex.Length == 0)55 {56 return new byte[] { 0 };57 }58 59 if (hex.Length % 2 == 1)60 {61 hex = "0" + hex;62 }63 64 byte[] result = new byte[hex.Length / 2];65 66 for (int i = 0; i < hex.Length / 2; i++)67 {68 result[i] = byte.Parse(hex.Substring(2 * i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);69 }70 71 return result;72 }
调用
1 SYSUser sysUser = Session["XXXX"] as SYSUser; 2 string username = sysUser.LoginName; 3 string linkInfo = string.Format("user={0};date={1}", username, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 4 string encryPassword = Encrypt(linkInfo, DateTime.Now.ToString("yyyy-MM-dd")); 5 string url = string.Format(this.divPilotHomeUrl.Value.Trim()+"/handler/SSOValidate.aspx?userKey={0}", encryPassword); 6 HttpBrowserCapabilities hbc = HttpContext.Current.Request.Browser; 7 string browserType = hbc.Browser.ToString(); //获取浏览器类型 8 string browserVersion = hbc.Version.ToString(); //获取版本号 9 if (browserType.IndexOf("Internet") > -1)10 {11 //string script = string.Format("",url);12 //this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", script, true);13 string script = string.Format("gotoUrl('{0}')", url);14 ClientScript.RegisterClientScriptBlock(this.GetType(), "gotoUrl", script, true); 15 }16 else17 {18 this.Response.Redirect(url, false);19 }