hytos / DTI_PID / SPPIDConverter / License.cs @ 7b476006
이력 | 보기 | 이력해설 | 다운로드 (11.4 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
using System.Security.Cryptography; |
7 |
using System.IO; |
8 |
using Microsoft.Win32; |
9 |
using System.Net.NetworkInformation; |
10 |
using Converter.SPPID.Form; |
11 |
using System.Windows.Forms; |
12 |
using Converter.SPPID.Properties; |
13 |
using System.Net; |
14 |
using JWT; |
15 |
using JWT.Algorithms; |
16 |
using JWT.Serializers; |
17 |
using JWT.Exceptions; |
18 |
using Newtonsoft.Json; |
19 | |
20 |
namespace DOFTECH.License |
21 |
{ |
22 |
public class LicenseDTO |
23 |
{ |
24 |
/// <summary> |
25 |
/// 라이센스 고유 값 |
26 |
/// </summary> |
27 |
public int uid { get; set; } |
28 | |
29 |
/// <summary> |
30 |
/// MAC 주소 |
31 |
/// </summary> |
32 |
public string mac { get; set; } |
33 | |
34 |
/// <summary> |
35 |
/// 메일 주소 |
36 |
/// </summary> |
37 |
public string email { get; set; } |
38 | |
39 |
/// <summary> |
40 |
/// 어플리케이션 |
41 |
/// </summary> |
42 |
public string app { get; set; } |
43 | |
44 |
/// <summary> |
45 |
/// 라이센스 코드 |
46 |
/// </summary> |
47 |
public string code { get; set; } |
48 | |
49 |
/// <summary> |
50 |
/// api name. |
51 |
/// </summary> |
52 |
public string api { get; set; } |
53 | |
54 |
/// <summary> |
55 |
/// 만료 시간. |
56 |
/// </summary> |
57 |
public string expiration_date { get; set; } |
58 | |
59 |
/// <summary> |
60 |
/// 만료 시간. |
61 |
/// </summary> |
62 |
public string exp { get; set; } |
63 | |
64 |
/// <summary> |
65 |
/// License 개수 |
66 |
/// </summary> |
67 |
public int count { get; set; } |
68 |
} |
69 | |
70 |
public class LicenseReturn |
71 |
{ |
72 |
/// <summary> |
73 |
/// STATUS |
74 |
/// </summary> |
75 |
public bool success { get; set; } |
76 | |
77 |
/// <summary> |
78 |
/// CODE |
79 |
/// </summary> |
80 |
public int code { get; set; } |
81 | |
82 |
/// <summary> |
83 |
/// MSG |
84 |
/// </summary> |
85 |
public string msg { get; set; } |
86 | |
87 |
/// <summary> |
88 |
/// DATA |
89 |
/// </summary> |
90 |
public string data { get; set; } |
91 |
} |
92 | |
93 |
public static class License |
94 |
{ |
95 |
//public static bool IsEnabled() |
96 |
//{ |
97 |
// bool result = false; |
98 |
// string path = string.Empty; |
99 | |
100 |
// try |
101 |
// { |
102 |
// RegistryKey key = Registry.LocalMachine; |
103 |
// RegistryKey software = key.OpenSubKey("SOFTWARE"); |
104 |
// RegistryKey DOFTECH = software.OpenSubKey("DOFTECH"); |
105 |
// RegistryKey SPPIDConverter = DOFTECH.OpenSubKey("SPPIDConverter", true); |
106 |
// path = SPPIDConverter.GetValue("path") + "licenses.licx"; |
107 |
// if (File.Exists(path)) |
108 |
// { |
109 |
// string data = File.ReadAllText(path); |
110 |
// data = Crypt.decrypt(data, "dof1073#"); |
111 |
// if (data.StartsWith("Date:")) |
112 |
// { |
113 |
// data = data.Remove(0, "Date:".Length); |
114 | |
115 |
// if (string.IsNullOrEmpty(data)) |
116 |
// { |
117 |
// DateTime dataTime = new DateTime(2000, 1, 1); |
118 |
// DateTime currentTime = DateTime.Now; |
119 |
// if (DateTime.TryParse(Converter.SPPID.Properties.Resources.lic, out dataTime) && DateTime.Compare(dataTime.AddDays(150), currentTime) >= 0) |
120 |
// result = true; |
121 |
// } |
122 |
// } |
123 |
// else if (data.StartsWith("Absolute:")) |
124 |
// { |
125 |
// data = data.Remove(0, "Absolute:".Length); |
126 |
// string macAddress = NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString(); |
127 |
// if (string.IsNullOrEmpty(data)) |
128 |
// { |
129 |
// data = "Absolute:" + macAddress; |
130 |
// data = Crypt.encrypt(data, "dof1073#"); |
131 |
// File.WriteAllText(path, data); |
132 |
// result = true; |
133 |
// } |
134 |
// else if (macAddress == data) |
135 |
// result = true; |
136 |
// } |
137 |
// } |
138 |
// } |
139 |
// catch (Exception ex) |
140 |
// { |
141 | |
142 |
// } |
143 | |
144 |
// return result; |
145 |
//} |
146 | |
147 |
public static bool IsEnabled() |
148 |
{ |
149 |
bool result = false; |
150 | |
151 |
try |
152 |
{ |
153 |
string key = Settings.Default.LicenseKey; |
154 |
if (!string.IsNullOrEmpty(key)) |
155 |
{ |
156 |
key = Crypt.decrypt(key, "dof1073#"); |
157 |
} |
158 |
else |
159 |
{ |
160 |
key = "dummy"; // must fail |
161 |
} |
162 | |
163 |
if (!CheckLicense(key)) |
164 |
{ |
165 |
var dlg = new LicenseForm(); |
166 |
if (dlg.ShowDialog() == DialogResult.OK) |
167 |
{ |
168 |
key = dlg.key; |
169 |
} |
170 | |
171 |
if (CheckLicense(key)) |
172 |
{ |
173 |
Settings.Default.LicenseKey = Crypt.encrypt(key, "dof1073#"); |
174 |
Settings.Default.Save(); |
175 | |
176 |
result = true; |
177 |
} |
178 |
} |
179 |
else |
180 |
{ |
181 |
result = true; |
182 |
} |
183 |
} |
184 |
catch (Exception ex) |
185 |
{ |
186 | |
187 |
} |
188 | |
189 |
return result; |
190 |
} |
191 | |
192 |
public static bool CheckLicense(string key) |
193 |
{ |
194 |
try |
195 |
{ |
196 |
const string secret = "795hi#(7qq5&p#f3ysa#n-449h8g_n95olca)b%t23s7!w%v0m"; |
197 | |
198 |
if (key != "dummy") |
199 |
{ |
200 |
IJsonSerializer serializer = new JsonNetSerializer(); |
201 |
var provider = new UtcDateTimeProvider(); |
202 |
IJwtValidator validator = new JwtValidator(serializer, provider); |
203 |
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); |
204 |
IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); // symmetric |
205 |
IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm); |
206 |
var json = decoder.Decode(key, secret, verify: true); |
207 |
var licenseDto = JsonConvert.DeserializeObject<LicenseDTO>(json); |
208 | |
209 |
if (NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString() != licenseDto.mac) |
210 |
{ |
211 |
return false; |
212 |
} |
213 | |
214 |
string url = licenseDto.api; |
215 |
string responseText = string.Empty; |
216 | |
217 |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); |
218 |
request.Method = "GET"; |
219 |
request.Timeout = 5 * 1000; |
220 |
request.ContentType = "application/json"; |
221 |
request.Headers.Add("x-auth-token:" + key); |
222 | |
223 |
bool validKey = true; |
224 | |
225 |
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse()) |
226 |
{ |
227 |
HttpStatusCode status = resp.StatusCode; |
228 |
Console.WriteLine(status); |
229 | |
230 |
Stream respStream = resp.GetResponseStream(); |
231 |
using (StreamReader sr = new StreamReader(respStream)) |
232 |
{ |
233 |
responseText = sr.ReadToEnd(); |
234 |
var licenseReturn = JsonConvert.DeserializeObject<LicenseReturn>(responseText); |
235 | |
236 |
if (!licenseReturn.success) |
237 |
{ |
238 |
MessageBox.Show(licenseReturn.msg, "SPPID Converter", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
239 |
validKey = false; |
240 |
} |
241 |
} |
242 |
} |
243 | |
244 |
return validKey; |
245 |
} |
246 |
} |
247 |
catch (Exception ex) |
248 |
{ |
249 | |
250 |
} |
251 | |
252 |
return false; |
253 |
} |
254 | |
255 |
public static string SHA256Hash(string data) |
256 |
{ |
257 | |
258 |
SHA256 sha = new SHA256Managed(); |
259 |
byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(data)); |
260 |
StringBuilder stringBuilder = new StringBuilder(); |
261 |
foreach (byte b in hash) |
262 |
{ |
263 |
stringBuilder.AppendFormat("{0:x2}", b); |
264 |
} |
265 |
return stringBuilder.ToString(); |
266 |
} |
267 | |
268 |
private static void CreateDateLicenseFile() |
269 |
{ |
270 |
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Date]licenses.licx"; |
271 |
string data = "Date:"; |
272 |
using (StreamWriter sw = File.CreateText(path)) |
273 |
{ |
274 |
data = Crypt.encrypt(data, "dof1073#"); |
275 |
sw.Write(data); |
276 |
sw.Close(); |
277 |
sw.Dispose(); |
278 |
} |
279 |
} |
280 | |
281 |
private static void CreateAbsoluteLicenseFile() |
282 |
{ |
283 |
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Absolute]licenses.licx"; |
284 |
string data = "Absolute:"; |
285 |
using (StreamWriter sw = File.CreateText(path)) |
286 |
{ |
287 |
data = Crypt.encrypt(data, "dof1073#"); |
288 |
sw.Write(data); |
289 |
sw.Close(); |
290 |
sw.Dispose(); |
291 |
} |
292 |
} |
293 |
} |
294 | |
295 |
class Crypt |
296 |
{ |
297 |
public static String encrypt(String text, String password) |
298 |
{ |
299 |
RijndaelManaged RijndaelCipher = new RijndaelManaged(); |
300 |
byte[] plainText = Encoding.UTF8.GetBytes(text); |
301 |
byte[] salt = Encoding.UTF8.GetBytes(password); |
302 |
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(password, salt); |
303 |
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)); |
304 |
MemoryStream memoryStream = new MemoryStream(); |
305 |
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write); |
306 |
cryptoStream.Write(plainText, 0, plainText.Length); // 암호화 시작 |
307 |
cryptoStream.FlushFinalBlock(); |
308 |
byte[] cryptBytes = memoryStream.ToArray(); |
309 |
// 스트림을 닫습니다. |
310 |
memoryStream.Close(); |
311 |
cryptoStream.Close(); |
312 |
String cryptResult = Convert.ToBase64String(cryptBytes); |
313 |
return cryptResult; // 암호화 문자열 리턴. |
314 |
} |
315 |
public static String decrypt(String text, String password) |
316 |
{ |
317 |
RijndaelManaged RijndaelCipher = new RijndaelManaged(); |
318 |
byte[] encryptData = Convert.FromBase64String(text); |
319 |
byte[] salt = Encoding.UTF8.GetBytes(password); |
320 |
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(password, salt); |
321 |
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); |
322 |
MemoryStream memoryStream = new MemoryStream(encryptData); |
323 |
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read); |
324 |
byte[] PlainText = new byte[encryptData.Length]; |
325 |
// 복호화 시작 |
326 |
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length); |
327 |
memoryStream.Close(); |
328 |
cryptoStream.Close(); |
329 |
// 복호화된 데이터를 문자열로 바꾼다. |
330 |
string DecryptedData = Encoding.UTF8.GetString(PlainText, 0, DecryptedCount); |
331 |
// 최종 결과 리턴 |
332 |
return DecryptedData; |
333 |
} |
334 |
} |
335 |
} |