1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Web.Security;
5
using System.Security.Cryptography;
6
using System.Text.RegularExpressions;
7
8
public class StringHelper
9
{
10
/// <summary>
11
///将字符串格式化为HTML
12
/// </summary>
13
/// <param name="normalStr">所要格式化的字符串</param>
14
/// <returns>返回格式化后的HTML代码</returns>
15
public static string FormatToHtml(string normalStr)
16
{
17
normalStr = System.Web.HttpContext.Current.Server.UrlDecode(normalStr);
18
normalStr = System.Web.HttpContext.Current.Server.HtmlEncode(normalStr);
19
20
StringBuilder html = new StringBuilder(normalStr);
21
22
html.Replace(" ", " ");
23
html.Replace("\r\n", "<br />");
24
25
return html.ToString();
26
}
27
28
/// <summary>
29
/// 将HTML转为普通文本格式
30
/// </summary>
31
/// <param name="htmlStr">所要转换的HTML字符串</param>
32
/// <returns>返回普通文本</returns>
33
public static string RestoreFromHtml(string htmlStr)
34
{
35
htmlStr = System.Web.HttpContext.Current.Server.HtmlDecode(htmlStr);
36
37
StringBuilder normalStr = new StringBuilder(htmlStr);
38
39
normalStr.Replace("<br />", "\r\n");
40
41
normalStr.Replace(""", "\"");
42
normalStr.Replace("<", "<");
43
normalStr.Replace(">", ">");
44
normalStr.Replace(" ", " ");
45
normalStr.Replace("&", "&");
46
47
return normalStr.ToString();
48
}
49
50
/// <summary>
51
///
52
/// </summary>
53
/// <param name="str"></param>
54
/// <param name="length"></param>
55
/// <returns></returns>
56
public static string CutString(string str, int length)
57
{
58
if (str != null && str.Length > length)
59
{
60
return string.Format("{0}
.", str.Substring(0, length));
61
}
62
else
63
{
64
return str;
65
}
66
}
67
68
/// <summary>
69
///
70
/// </summary>
71
/// <param name="withOut"></param>
72
/// <returns></returns>
73
public static string GetCurrentQueryString(string withOut)
74
{
75
string _getkeys = "";
76
string _result = "";
77
78
if (System.Web.HttpContext.Current.Request.QueryString != null)
79
{
80
for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
81
{
82
_getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
83
if (_getkeys != withOut)
84
{
85
_result = string.Format("{0}&{1}={2}", _result, _getkeys, System.Web.HttpContext.Current.Request.QueryString[_getkeys]);
86
}
87
}
88
}
89
90
return _result;
91
}
92
93
/// <summary>
94
/// 对字符串进行加密(不可逆)
95
/// 0 is SHA1,1 is MD5
96
/// </summary>
97
/// <param name="Password">要加密的字符串</param>
98
/// <param name="Format">加密方式,0 is SHA1,1 is MD5</param>
99
/// <returns></returns>
100
public static string HashEncrypt(string Password, int Format)
101
{
102
string strResult = "";
103
switch (Format)
104
{
105
case 0:
106
strResult = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "SHA1");
107
break;
108
case 1:
109
strResult = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
110
break;
111
default:
112
strResult = Password;
113
break;
114
}
115
116
return strResult;
117
}
118
119
/// <summary>
120
/// 使用SHA1Managed类产生长度为160位哈希值。不需要提供密钥。
121
/// </summary>
122
/// <returns></returns>
123
public string SHA1ManagedHasher(string hashText)
124
{
125
byte[] SHA1Data = System.Text.Encoding.UTF8.GetBytes(hashText);
126
127
SHA1Managed Sha1 = new SHA1Managed();
128
129
byte[] Result = Sha1.ComputeHash(SHA1Data);
130
131
return Convert.ToBase64String(Result); //返回长度为28字节的字符串
132
}
133
134
防SQL注入式攻击代码
209
}
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Web.Security;5
using System.Security.Cryptography;6
using System.Text.RegularExpressions;7

8
public class StringHelper9
{10
/// <summary>11
///将字符串格式化为HTML12
/// </summary>13
/// <param name="normalStr">所要格式化的字符串</param>14
/// <returns>返回格式化后的HTML代码</returns>15
public static string FormatToHtml(string normalStr)16
{17
normalStr = System.Web.HttpContext.Current.Server.UrlDecode(normalStr);18
normalStr = System.Web.HttpContext.Current.Server.HtmlEncode(normalStr);19

20
StringBuilder html = new StringBuilder(normalStr);21

22
html.Replace(" ", " ");23
html.Replace("\r\n", "<br />");24

25
return html.ToString();26
}27

28
/// <summary>29
/// 将HTML转为普通文本格式30
/// </summary>31
/// <param name="htmlStr">所要转换的HTML字符串</param>32
/// <returns>返回普通文本</returns>33
public static string RestoreFromHtml(string htmlStr)34
{35
htmlStr = System.Web.HttpContext.Current.Server.HtmlDecode(htmlStr);36

37
StringBuilder normalStr = new StringBuilder(htmlStr);38

39
normalStr.Replace("<br />", "\r\n");40

41
normalStr.Replace(""", "\"");42
normalStr.Replace("<", "<");43
normalStr.Replace(">", ">");44
normalStr.Replace(" ", " ");45
normalStr.Replace("&", "&");46

47
return normalStr.ToString();48
}49

50
/// <summary>51
/// 52
/// </summary>53
/// <param name="str"></param>54
/// <param name="length"></param>55
/// <returns></returns>56
public static string CutString(string str, int length)57
{58
if (str != null && str.Length > length)59
{60
return string.Format("{0}
.", str.Substring(0, length));61
}62
else63
{64
return str;65
}66
}67

68
/// <summary>69
/// 70
/// </summary>71
/// <param name="withOut"></param>72
/// <returns></returns>73
public static string GetCurrentQueryString(string withOut)74
{75
string _getkeys = "";76
string _result = "";77

78
if (System.Web.HttpContext.Current.Request.QueryString != null)79
{80
for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)81
{82
_getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];83
if (_getkeys != withOut)84
{85
_result = string.Format("{0}&{1}={2}", _result, _getkeys, System.Web.HttpContext.Current.Request.QueryString[_getkeys]);86
}87
}88
}89

90
return _result;91
}92

93
/// <summary>94
/// 对字符串进行加密(不可逆)95
/// 0 is SHA1,1 is MD596
/// </summary>97
/// <param name="Password">要加密的字符串</param>98
/// <param name="Format">加密方式,0 is SHA1,1 is MD5</param>99
/// <returns></returns>100
public static string HashEncrypt(string Password, int Format)101
{102
string strResult = "";103
switch (Format)104
{105
case 0:106
strResult = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "SHA1");107
break;108
case 1:109
strResult = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");110
break;111
default:112
strResult = Password;113
break;114
}115

116
return strResult;117
}118

119
/// <summary>120
/// 使用SHA1Managed类产生长度为160位哈希值。不需要提供密钥。121
/// </summary>122
/// <returns></returns>123
public string SHA1ManagedHasher(string hashText)124
{125
byte[] SHA1Data = System.Text.Encoding.UTF8.GetBytes(hashText);126

127
SHA1Managed Sha1 = new SHA1Managed();128

129
byte[] Result = Sha1.ComputeHash(SHA1Data);130

131
return Convert.ToBase64String(Result); //返回长度为28字节的字符串132
}133

134
防SQL注入式攻击代码209
}
浙公网安备 33010602011771号