프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / License.cs @ fde5f207

이력 | 보기 | 이력해설 | 다운로드 (11.9 KB)

1 91c7a82a gaqhf
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 7b476006 esham21
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 91c7a82a gaqhf
20
namespace DOFTECH.License
21
{
22 7b476006 esham21
    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 91c7a82a gaqhf
    public static class License
94
    {
95 7b476006 esham21
        //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 91c7a82a gaqhf
        public static bool IsEnabled()
148
        {
149
            bool result = false;
150
151 6b39cb1a esham21
            return true;
152 7e8eccb1 esham21
153 91c7a82a gaqhf
            try
154
            {
155 7b476006 esham21
                string key = Settings.Default.LicenseKey;
156
                if (!string.IsNullOrEmpty(key))
157
                {
158
                    key = Crypt.decrypt(key, "dof1073#");
159
                }
160
                else
161 91c7a82a gaqhf
                {
162 7b476006 esham21
                    key = "dummy"; // must fail
163
                }
164
165
                if (!CheckLicense(key))
166
                {
167
                    var dlg = new LicenseForm();
168
                    if (dlg.ShowDialog() == DialogResult.OK)
169 91c7a82a gaqhf
                    {
170 7b476006 esham21
                        key = dlg.key;
171
                    }
172 91c7a82a gaqhf
173 7b476006 esham21
                    if (CheckLicense(key))
174
                    {
175
                        Settings.Default.LicenseKey = Crypt.encrypt(key, "dof1073#");
176
                        Settings.Default.Save();
177
178
                        result = true;
179 91c7a82a gaqhf
                    }
180 8579be27 esham21
                    else
181
                    {
182
                        Settings.Default.LicenseKey = Crypt.encrypt("dummy", "dof1073#");
183
                        Settings.Default.Save();
184
                    }
185 7b476006 esham21
                }
186
                else
187
                {
188
                    result = true;
189
                }
190
            }
191
            catch (Exception ex)
192
            {
193 4d4dce52 esham21
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
194 7b476006 esham21
            }
195
196
            return result;
197
        }
198
199
        public static bool CheckLicense(string key)
200
        {
201 8579be27 esham21
            // return false; // for erase key
202 7b476006 esham21
            try
203
            {
204
                const string secret = "795hi#(7qq5&p#f3ysa#n-449h8g_n95olca)b%t23s7!w%v0m";
205
206
                if (key != "dummy")
207
                {
208
                    IJsonSerializer serializer = new JsonNetSerializer();
209
                    var provider = new UtcDateTimeProvider();
210
                    IJwtValidator validator = new JwtValidator(serializer, provider);
211
                    IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
212
                    IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); // symmetric
213
                    IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);
214
                    var json = decoder.Decode(key, secret, verify: true);
215
                    var licenseDto = JsonConvert.DeserializeObject<LicenseDTO>(json);
216
217
                    if (NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString() != licenseDto.mac)
218 0ee10e77 esham21
                    //if ("00155D0B4A1A" != licenseDto.mac)
219 7b476006 esham21
                    {
220
                        return false;
221
                    }
222
223
                    string url = licenseDto.api;
224
                    string responseText = string.Empty;
225
226
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
227
                    request.Method = "GET";
228
                    request.Timeout = 5 * 1000;
229
                    request.ContentType = "application/json";
230
                    request.Headers.Add("x-auth-token:" + key);
231
232
                    bool validKey = true;
233
234
                    using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
235 91c7a82a gaqhf
                    {
236 7b476006 esham21
                        HttpStatusCode status = resp.StatusCode;
237
                        Console.WriteLine(status);
238
239
                        Stream respStream = resp.GetResponseStream();
240
                        using (StreamReader sr = new StreamReader(respStream))
241 91c7a82a gaqhf
                        {
242 7b476006 esham21
                            responseText = sr.ReadToEnd();
243
                            var licenseReturn = JsonConvert.DeserializeObject<LicenseReturn>(responseText);
244
245
                            if (!licenseReturn.success)
246
                            {
247
                                MessageBox.Show(licenseReturn.msg, "SPPID Converter", MessageBoxButtons.OK, MessageBoxIcon.Warning);
248
                                validKey = false;
249
                            }
250 91c7a82a gaqhf
                        }
251
                    }
252 7b476006 esham21
253
                    return validKey;
254 91c7a82a gaqhf
                }
255
            }
256
            catch (Exception ex)
257
            {
258 4d4dce52 esham21
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
259 91c7a82a gaqhf
            }
260
261 7b476006 esham21
            return false;
262 91c7a82a gaqhf
        }
263
264
        public static string SHA256Hash(string data)
265
        {
266
267
            SHA256 sha = new SHA256Managed();
268
            byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(data));
269
            StringBuilder stringBuilder = new StringBuilder();
270
            foreach (byte b in hash)
271
            {
272
                stringBuilder.AppendFormat("{0:x2}", b);
273
            }
274
            return stringBuilder.ToString();
275
        }
276
277
        private static void CreateDateLicenseFile()
278
        {
279
            string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Date]licenses.licx";
280
            string data = "Date:";
281
            using (StreamWriter sw = File.CreateText(path))
282
            {
283
                data = Crypt.encrypt(data, "dof1073#");
284
                sw.Write(data);
285
                sw.Close();
286
                sw.Dispose();
287
            }
288
        }
289
290
        private static void CreateAbsoluteLicenseFile()
291
        {
292
            string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Absolute]licenses.licx";
293
            string data = "Absolute:";
294
            using (StreamWriter sw = File.CreateText(path))
295
            {
296
                data = Crypt.encrypt(data, "dof1073#");
297
                sw.Write(data);
298
                sw.Close();
299
                sw.Dispose();
300
            }
301
        }
302
    }
303
304
    class Crypt
305
    {
306
        public static String encrypt(String text, String password)
307
        {
308
            RijndaelManaged RijndaelCipher = new RijndaelManaged();
309
            byte[] plainText = Encoding.UTF8.GetBytes(text);
310
            byte[] salt = Encoding.UTF8.GetBytes(password);
311
            PasswordDeriveBytes secretKey = new PasswordDeriveBytes(password, salt);
312
            ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
313
            MemoryStream memoryStream = new MemoryStream();
314
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
315
            cryptoStream.Write(plainText, 0, plainText.Length);     // 암호화 시작
316
            cryptoStream.FlushFinalBlock();
317
            byte[] cryptBytes = memoryStream.ToArray();
318
            // 스트림을 닫습니다.
319
            memoryStream.Close();
320
            cryptoStream.Close();
321
            String cryptResult = Convert.ToBase64String(cryptBytes);
322
            return cryptResult;     // 암호화 문자열 리턴.
323
        }
324
        public static String decrypt(String text, String password)
325
        {
326
            RijndaelManaged RijndaelCipher = new RijndaelManaged();
327
            byte[] encryptData = Convert.FromBase64String(text);
328
            byte[] salt = Encoding.UTF8.GetBytes(password);
329
            PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(password, salt);
330
            ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
331
            MemoryStream memoryStream = new MemoryStream(encryptData);
332
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
333
            byte[] PlainText = new byte[encryptData.Length];
334
            // 복호화 시작
335
            int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
336
            memoryStream.Close();
337
            cryptoStream.Close();
338
            // 복호화된 데이터를 문자열로 바꾼다.
339
            string DecryptedData = Encoding.UTF8.GetString(PlainText, 0, DecryptedCount);
340
            // 최종 결과 리턴
341
            return DecryptedData;
342
        }
343
    }
344
}
클립보드 이미지 추가 (최대 크기: 500 MB)