LoginPasswordHelp

简介: 加密解密 1 public static string Encrypt(string Text, string sKey) 2 { 3 DESCryptoServiceProvider des = new DESCryptoServicePr...

加密解密

 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("<script>gotoUrl('{0}');</script>",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             else
17             {
18                 this.Response.Redirect(url, false);
19             }
View Code

 

目录
相关文章
Gitlab报错:No authentication methods configured on login page
Gitlab报错:No authentication methods configured on login page
160 0
HttpMessageNotReadableException: Required request body is missing: xxx.controller.login(xxx)...
HttpMessageNotReadableException: Required request body is missing: xxx.controller.login(xxx)...
HttpMessageNotReadableException: Required request body is missing: xxx.controller.login(xxx)...
|
开发工具 git
Incorrect username or password (access token)
Incorrect username or password (access token)
179 0
Incorrect username or password (access token)
|
Java Linux 程序员
记录:Could not resolve placeholder 'user.userName' in value "${xxx.xx}"...【亲测有效】
记录:Could not resolve placeholder 'user.userName' in value "${xxx.xx}"...【亲测有效】
855 0
【laravle】 Route [login] not defined.
【laravle】 Route [login] not defined.
90 0
【laravle】 Route [login] not defined.
|
NoSQL Redis 数据安全/隐私保护
AUTH password
为redis服务请求设置一个密码。redis可以设置在客户端执行commands请求前需要通过密码验证。通过修改配置文件的requirepass就可以设置密码。 如果密码与配置文件里面设置的密码一致,服务端就会发会一个OK的状态码,接受客户端发送其他的请求命令,否则服务端会返回一个错误码,客户端需要尝试使用新的密码来进行连接。
1132 0
|
网络安全
|
网络安全
|
数据安全/隐私保护 网络架构 网络安全