博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LoginPasswordHelp
阅读量:5170 次
发布时间:2019-06-13

本文共 4612 字,大约阅读时间需要 15 分钟。

加密解密

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         }
View Code

 调用

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             }
View Code

 

转载于:https://www.cnblogs.com/yidengbone/p/6519951.html

你可能感兴趣的文章
Spring Mvc模式下Jquery Ajax 与后台交互操作
查看>>
(转)matlab练习程序(HOG方向梯度直方图)
查看>>
面试整理:Python基础
查看>>
Program exited with code **** 相关解释
查看>>
tableView
查看>>
Happy Great BG-卡精度
查看>>
Xamarin Visual Studio不识别JDK路径
查看>>
菜鸟“抄程序”之道
查看>>
Ubuntu下关闭防火墙
查看>>
TCP/IP 邮件的原理
查看>>
原型设计工具
查看>>
windows下的C++ socket服务器(4)
查看>>
css3 2d转换3d转换以及动画的知识点汇总
查看>>
【Java】使用Eclipse进行远程调试,Linux下开启远程调试
查看>>
计算机改名导致数据库链接的诡异问题
查看>>
Java8内存模型—永久代(PermGen)和元空间(Metaspace)(转)
查看>>
ObjectiveC基础教程(第2版)
查看>>
centos 引导盘
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
Apriori算法
查看>>