1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Web;
5
using System.Text.RegularExpressions;
6
using System.Security.Cryptography;
7
using System.Globalization;
8
9
namespace MyUtility
10
...{
11
public class MyConvert
12
...{
13
对字符串进行加密#region 对字符串进行加密
14
/**//// <summary>
15
/// 对字符串进行加密,不可逆
16
/// </summary>
17
/// <param name="str">要加密的字符串</param>
18
/// <param name="format">要使用的哈希算法:md5或sha1</param>
19
/// <returns></returns>
20
public static string Encrypt(string str, string format)
21
...{
22
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, format);
23
}
24
#endregion
25
26
替换字符串,掉特殊字符#region 替换字符串,掉特殊字符
27
/**//// <summary>
28
/// 替换字符串,掉特殊字符
29
/// </summary>
30
/// <param name="text"></param>
31
/// <param name="maxLength"></param>
32
/// <returns></returns>
33
public static string ReplaceString(string text, int maxLength)
34
...{
35
if (string.IsNullOrEmpty(text))
36
return string.Empty;
37
if (text.Length > maxLength)
38
text = text.Substring(0, maxLength);
39
text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
40
text = Regex.Replace(text, " ", " ");
41
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
42
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
43
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
44
text = Regex.Replace(text, "\\(|\\)", " ");
45
text = Regex.Replace(text, "\\[|\\]|[|]|(|)|〔|〕|【|】|〈|〉|「|」|『|』|{|}|﹛|﹜|﹝|﹞|《|》|\\<|\\>|\\?|?", "");
46
text = text.Replace(">", "");
47
text = text.Replace("<", "");
48
text = text.Replace("'", " ");
49
return GetBanJiao(text);
50
}
51
#endregion
52
53
全角字符转换为半角字符#region 全角字符转换为半角字符
54
/**//// <summary>
55
/// 全角字符转换为半角字符
56
/// </summary>
57
/// <param name="quanJianString"></param>
58
/// <returns></returns>
59
public static string GetBanJiao(string quanJianString)
60
...{
61
char[] c = quanJianString.ToCharArray();
62
for (int i = 0; i < c.Length; i++)
63
...{
64
byte[] b = System.Text.Encoding.Unicode.GetBytes(c, i, 1);
65
if (b.Length == 2)
66
...{
67
if (b[1] == 255)
68
...{
69
b[0] = (byte)(b[0] + 32);
70
b[1] = 0;
71
c[i] = System.Text.Encoding.Unicode.GetChars(b)[0];
72
}
73
}
74
}
75
return new string(c);
76
}
77
#endregion
78
79
创建Key#region 创建Key
80
/**//// <summary>
81
/// 创建Key
82
/// </summary>
83
/// <returns></returns>
84
public string GenerateKey()
85
...{
86
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
87
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
88
}
89
#endregion
90
91
加密字符串#region 加密字符串
92
/**//// <summary>
93
/// 加密字符串
94
/// </summary>
95
/// <param name="sInputString">需加密的字符串</param>
96
/// <param name="sKey">密钥,长度需8位</param>
97
/// <returns></returns>
98
public static string EncryptString(string sInputString, string sKey)
99
...{
100
byte[] data = Encoding.UTF8.GetBytes(sInputString);
101
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
102
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
103
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
104
ICryptoTransform desencrypt = DES.CreateEncryptor();
105
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
106
return BitConverter.ToString(result);
107
}
108
#endregion
109
110
解密字符串#region 解密字符串
111
/**//// <summary>
112
/// 解密字符串
113
/// </summary>
114
/// <param name="sInputString">需解密的字符串</param>
115
/// <param name="sKey">密钥,长度需8位</param>
116
/// <returns></returns>
117
public static string DecryptString(string sInputString, string sKey)
118
...{
119
string[] sInput = sInputString.Split("-".ToCharArray());
120
byte[] data = new byte[sInput.Length];
121
for (int i = 0; i < sInput.Length; i++)
122
...{
123
data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
124
}
125
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
126
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
127
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
128
ICryptoTransform desencrypt = DES.CreateDecryptor();
129
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
130
return Encoding.UTF8.GetString(result);
131
}
132
#endregion
133
}
134
}
135