1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 |
using
System; using
System.Security.Cryptography; using
System.Text; using
System.Text.RegularExpressions; using
System.Web; using
System.Web.Security; namespace
ZooleeBasic { public
class ZConvert { /// <summary> /// 替换字符型sql参数中的有问题符号 /// </summary> /// <param name="str"></param> /// <returns></returns> public
static string Killw( string
str) { return
(ZValid.IsNothing(str)) ? ""
: (str.Trim()).Replace( "‘" , "'" ); } /// <summary> /// 替换字符型sql参数中的有问题符号(指定最大长度) /// </summary> /// <param name="str"></param> /// <param name="maxlen">指定最大长度</param> /// <returns></returns> public
static string Killw( string
str, int
maxlen) { string
re = Killw(str); if
(re == "" ) { return
re; } int
len = re.Length; if
(maxlen > len) { maxlen = len; } return
re.Substring(0, maxlen); } /// <summary> /// 替换数值型sql参数中的有问题符号 /// </summary> /// <param name="str"></param> /// <returns></returns> public
static string Killn( string
str) { return
(str == null
|| !ZValid.IsNumber(str.Trim())) ? "0"
: str.Trim(); } /// <summary> /// Html转义成实体 /// </summary> /// <param name="str">string</param> /// <returns>string</returns> public
static string HtmlEncode( string
str) { StringBuilder sb = new
StringBuilder(str); sb = sb.Replace( "&" , "&" ); sb = sb.Replace( "\‘" , "‘" ); sb = sb.Replace( "\"" , "" "); sb = sb.Replace( " " , " " ); sb = sb.Replace( "<" , "<" ); sb = sb.Replace( ">" , ">" ); sb = sb.Replace( "\\n" , "<br/>" ); return
sb.ToString(); } /// <summary> /// 实体解析成Html /// </summary> /// <param name="str">string</param> /// <returns>string</returns> public
static string HtmlDecode( string
str) { StringBuilder sb = new
StringBuilder(str); sb = sb.Replace( "<br/>" , "\\n" ); sb = sb.Replace( ">" , ">" ); sb = sb.Replace( "<" , "<" ); sb = sb.Replace( " " , " " ); sb = sb.Replace( "" ", " \ "" ); sb = sb.Replace( "‘" , "\‘" ); sb = sb.Replace( "&" , "&" ); return
sb.ToString(); } /// <summary> /// 移除Html标记 /// </summary> /// <param name="content"></param> /// <returns></returns> public
static string NoHtml( string
content) { return
Regex.Replace(content, @"<[^>]*>" , string .Empty, RegexOptions.IgnoreCase); } /// <summary> /// URL编码 /// </summary> /// <param name="urlStr"></param> /// <returns></returns> public
static string UrlEncode( string
urlStr) { return
HttpUtility.UrlEncode(urlStr); } /// <summary> /// URL解码 /// </summary> /// <param name="urlStr"></param> /// <returns></returns> public
static string UrlDecode( string
urlStr) { return
HttpUtility.UrlDecode(urlStr); } /// <summary> /// Base64编码 /// </summary> /// <param name="str">原文</param> /// <returns>密文</returns> public
static string EnBase64( string
str) { return
Convert.ToBase64String(Encoding.UTF8.GetBytes(str)); } /// <summary> /// Base64解码 /// </summary> /// <param name="eStr">密文</param> /// <returns>原文</returns> public
static string DeBase64( string
eStr) { if
(!ZValid.IsBase64(eStr)){ return
eStr;} return
Encoding.UTF8.GetString(Convert.FromBase64String(eStr)); } /// <summary> /// MD5加密 /// </summary> /// <param name="source">源字符串</param> /// <returns>32位散列</returns> public
static string Encypt( string
source) { return
FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5" ); } /// <summary> /// AES加密 /// </summary> /// <param name="encryptString">原文</param> /// <param name="encryptKey">密钥</param> /// <returns>密文</returns> public
static string EnAES( string
encryptString, string
encryptKey) { byte [] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F }; encryptKey = ZString.PadString(encryptKey, 32, "" ); encryptKey = encryptKey.PadRight(32, ‘ ‘ ); RijndaelManaged rijndaelProvider = new
RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32)); rijndaelProvider.IV = Keys; ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor(); byte [] inputData = Encoding.UTF8.GetBytes(encryptString); byte [] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length); return
Convert.ToBase64String(encryptedData); } /// <summary> /// AES解密 /// </summary> /// <param name="decryptString">密文</param> /// <param name="decryptKey">密钥</param> /// <returns>原文</returns> public
static string DeAES( string
decryptString, string
decryptKey) { byte [] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F }; if
(decryptString == string .Empty) { return
string .Empty; } try { decryptKey = ZString.PadString(decryptKey, 32, "" ); decryptKey = decryptKey.PadRight(32, ‘ ‘ ); RijndaelManaged rijndaelProvider = new
RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(decryptKey); rijndaelProvider.IV = Keys; ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor(); byte [] inputData = Convert.FromBase64String(decryptString); byte [] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length); return
Encoding.UTF8.GetString(decryptedData); } catch { return
"" ; } } } } |
发一些自己最常用的C#函数(一) :用户提交过滤,布布扣,bubuko.com
原文:http://www.cnblogs.com/apollosun123/p/3639589.html