forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptHelper.cs
More file actions
180 lines (174 loc) · 6.36 KB
/
Copy pathEncryptHelper.cs
File metadata and controls
180 lines (174 loc) · 6.36 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using CYQ.Data;
namespace CYQ.Data.Tool
{
/// <summary>
/// 密码加密类
/// </summary>
public static class EncryptHelper
{
internal static byte[] GetHash(string key)
{
using (MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider())
{
return hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
}
}
private static byte[] _DefaultHashKey;
internal static byte[] DefaultHashKey
{
get
{
if (_DefaultHashKey == null)
{
_DefaultHashKey = GetHash("!1@2#3$4%5^6");
}
return _DefaultHashKey;
}
}
///// <summary>
///// 预留的二次加密
///// </summary>
//internal static string EncryptKey
//{
// get
// {
// return AppConfig.GetApp("EncryptKey", "");
// }
//}
#region
/// <summary>
/// 加密
/// </summary>
/// <param name="text">加密的内容</param>
/// <returns></returns>
public static string Encrypt(string text)
{
return Encrypt(text, "");
}
/// <param name="key">指定加密的key</param>
public static string Encrypt(string text, string key)
{
string result = Encrypt(text, DefaultHashKey);
if (string.IsNullOrEmpty(key))
{
key = AppConfig.GetApp("EncryptKey", "");
}
if (!string.IsNullOrEmpty(key))
{
result = Encrypt(result, GetHash(key)) + "=2";//设置二级加密标识
}
return result;
}
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name="text">要加密的字符串</param>
/// <param name="hashKey">密钥</param>
/// <returns>加密后并经base64编码的字符串</returns>
/// <remarks>静态方法,采用默认ascii编码</remarks>
private static string Encrypt(string text, byte[] hashKey)
{
string result = string.Empty;
using (TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider())
{
DES.Key = hashKey;
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.UTF8.GetBytes(text);
string pass = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
result = pass.Replace('=', '#');
}
return result;
}//end method
/// <summary>
/// 解密
/// </summary>
/// <param name="text">要解密的字符串</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string text)
{
return Decrypt(text, "");
}
/// <param name="key">指定加密的key</param>
public static string Decrypt(string text, string key)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
else
{
text = text.Trim().Replace(' ', '+');//处理Request的+号变空格问题。
if (string.IsNullOrEmpty(key))
{
key = AppConfig.GetApp("EncryptKey", "");
}
if (!string.IsNullOrEmpty(key) && text.EndsWith("=2"))
{
text = Decrypt(text.Substring(0, text.Length - 2), GetHash(key));//先解一次Key
}
return Decrypt(text, DefaultHashKey);
}
}
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name="text">要解密的字符串</param>
/// <param name="key">密钥</param>
/// <returns>解密后的字符串</returns>
/// <exception cref="">密钥错误</exception>
/// <remarks>静态方法,采用默认ascii编码</remarks>
private static string Decrypt(string text, byte[] hashKey)
{
string result = "";
text = text.Replace('#', '=');
using (TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider())
{
DES.Key = hashKey;
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
try
{
byte[] Buffer = Convert.FromBase64String(text);
result = ASCIIEncoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
catch
{
return text;
}
}
return result;
}
internal static bool HashKeyIsValid()
{
string acKey = Decrypt(AppConst.ACKey, AppConst.Host);
acKey = AppConfig.GetConn(acKey);
if (!string.IsNullOrEmpty(acKey))
{
string alKey = EncryptHelper.Decrypt(AppConst.ALKey, AppConst.Host);
if (!string.IsNullOrEmpty(alKey))
{
string code = AppConfig.GetApp(alKey);
if (!string.IsNullOrEmpty(code))
{
string[] items = EncryptHelper.Decrypt(code.Substring(4), code.Substring(0, 4)).Split(',');
DateTime d;
if ((DateTime.TryParse(items[0], out d) && d > DateTime.Now) || (items.Length > 1 && items[1] == AppConst.HNKey))
{
AppConfig.SetApp(alKey + AppConst.Result, "1");
return true;
}
}
}
AppConfig.SetApp(alKey + AppConst.Result, "0");
return false;
}
return true;
}
#endregion
}
}