This repository has been archived by the owner on Sep 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSecurity.cs
189 lines (185 loc) · 7.88 KB
/
OSecurity.cs
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
using System;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
namespace Ocrosoft
{
/// <summary>
/// 加解密及信息摘要功能类
/// </summary>
public class OSecurity
{
private static string RSA_PublicKey = @"<RSAKeyValue><Modulus>MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbIGP2E/PxI0EEdIPMQTArwH+1505lzC20EBCSy8aqMcJVZx5AbvYj/nbI4v+N7xlmS4kciOqj1zaGj9QoivKVjsBLW6Hmhe8QCCw+MmR6jmWwLgmNAakOoDmlFjO8HVRZzTZEjiZX5LX9NG9FgYTGmZlJbnv48irG4BXyMpOdzQIDAQAB</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
private static string RSA_PrivateKey = @"<RSAKeyValue><Modulus>MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJsgY/YT8/EjQQR0g8xBMCvAf7XnTmXMLbQQEJLLxqoxwlVnHkBu9iP+dsji/43vGWZLiRyI6qPXNoaP1CiK8pWOwEtboeaF7xAILD4yZHqOZbAuCY0BqQ6gOaUWM7wdVFnNNkSOJlfktf00b0WBhMaZmUlue/jyKsbgFfIyk53NAgMBAAECgYB8oeNuG83MGVTtbWdOvbkkDb8NuM9F/mth1d5a8pmkt+G4l+a4Qe5EMPfiom5L7KPtihaY9HAAPrKyHfCIukn3Gprv4ysrFKm2wdiBh6VojJ+0L3X2IwEVQ/BZLuT37Qis7P7WXVE1tSV/J6nLW9fUZg+ZXUmlzFAx2EiOWlK6AQJBAM4GFUGOFpyRvED0mNUdP3Jbx3fcf1v3/why4Deqx003YiUMjXQpjLseb1vvvnSfZa34gqnFPKG/fFsU7owoLI0CQQDAwaDUlegMCTWAD89yz+Ea98UlVCUqH+ldCVfC9RdhmT0OwrGvQDHEUmgBKKFTdM3n7FphycAMXKLvrrVb6QZBAkEAjC/TcuHuPOdlg4VsIUdfjr8owUSGXNwo62TPcNGB/+a5n6Ak+G/1VLXm7FX78Hstwu0ga8jL8vvK8GcT0sbbWQJANKTtcwIaJSdiuD4ZL0c9OKtQ6bgIim+6wZEqqfFcWGiMt3pPIwkKTo8fHqnlHbD6B4ySxsBeNkIashFqMNb8wQJBAKIQqEyTf+dynbjKdCccE7bALo1plmNgU82bU7vMOzHYHDsTAZ2CDmgAz78BZZbPiXDPOTOhUXESmK2lfiIIg4M=</D></RSAKeyValue>";
private static string AES_Key = "chenyanhong";
/// <summary>
/// RSA加密
/// </summary>1
/// <param name="content">加密内容</param>
/// <returns></returns>
public static string RSAEncrypt(string content)
{
String publickey = RSA_PublicKey;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publickey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherbytes);
}
/// <summary>
/// RSA解密
/// </summary>
/// <param name="content">解密内容</param>
/// <returns></returns>
public static string RSADecrypt(string privatekey, string content)
{
privatekey = RSA_PrivateKey;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(privatekey);
cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
return Encoding.UTF8.GetString(cipherbytes);
}
/// <summary>
/// MD5加密
/// </summary>
/// <param name="content">加密内容</param>
/// <returns></returns>
public static string MD5(string content)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(Encoding.Default.GetBytes(content));
return Encoding.Default.GetString(result);
}
/// <summary>
/// 获取AES32位密钥
/// </summary>
/// <returns></returns>
private static byte[] GetAesKey()
{
var key = AES_Key;
if (key.Length < 32)
{
key = key.PadRight(32, '0');
}
if (key.Length > 32)
{
key = key.Substring(0, 32);
}
return Encoding.UTF8.GetBytes(key);
}
/// <summary>
/// AES加密,32位0补全,ECB,PKCS7,BASE64
/// </summary>
/// <param name="content">加密内容</param>
/// <returns></returns>
public static string AESEncrypt(string content)
{
using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
{
aesProvider.Key = GetAesKey();
aesProvider.Mode = CipherMode.ECB;
aesProvider.Padding = PaddingMode.PKCS7;
using (ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor())
{
byte[] inputBuffers = Encoding.UTF8.GetBytes(content);
byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
aesProvider.Clear();
aesProvider.Dispose();
return Convert.ToBase64String(results, 0, results.Length);
}
}
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="content">解密内容</param>
/// <returns></returns>
public static string AESDecrypt(string content)
{
using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
{
aesProvider.Key = GetAesKey();
aesProvider.Mode = CipherMode.ECB;
aesProvider.Padding = PaddingMode.PKCS7;
using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor())
{
byte[] inputBuffers = Convert.FromBase64String(content);
byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
aesProvider.Clear();
return Encoding.UTF8.GetString(results);
}
}
}
// <summary>
/// SHA1加密
/// </summary>
/// <param name="content">要加密的内容</param>
/// <returns></returns>
public static string SHA1(string content)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] bytes_in = Encoding.UTF8.GetBytes(content);
byte[] bytes_out = sha1.ComputeHash(bytes_in);
sha1.Dispose();
string result = BitConverter.ToString(bytes_out);
result = result.Replace("-", "");
return result.ToLower();
}
/// <summary>
/// DateTime转UNIX时间戳
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static long DateTimeToTimeStamp(DateTime time)
{
DateTime startTime = TimeZone.CurrentTimeZone
.ToLocalTime(new DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
/// <summary>
/// 获得一个由数字大小写字母组成的随机字符串
/// </summary>
/// <param name="length">字符串长度</param>
/// <returns></returns>
public static string GetRandomString(int length)
{
byte[] b = new byte[4];
new RNGCryptoServiceProvider().GetBytes(b);
Random r = new Random(BitConverter.ToInt32(b, 0));
string s = null;
var str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < length; i++)
{
s += str.Substring(r.Next(0, str.Length - 1), 1);
}
return s;
}
/// <summary>
/// 检查手机号是否正确
/// </summary>
/// <param name="phone">手机号</param>
/// <returns></returns>
public static bool ValidPhone(string phone)
{
if(Regex.IsMatch(phone, "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$"))
{
return true;
}
return false;
}
/// <summary>
/// 检查身份证是否正确
/// </summary>
/// <param name="IDCard">身份证</param>
/// <returns></returns>
public static bool ValidIDCard(string IDCard)
{
if(Regex.IsMatch(IDCard, "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$"))
{
return true;
}
return false;
}
}
}