hytos / DTI_PID / SPPIDConverter / License.cs @ 619725c9
이력 | 보기 | 이력해설 | 다운로드 (6.47 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 |
|
11 |
namespace DOFTECH.License |
12 |
{ |
13 |
public static class License |
14 |
{ |
15 |
public static bool IsEnabled() |
16 |
{ |
17 |
bool result = false; |
18 |
string path = string.Empty; |
19 |
|
20 |
try |
21 |
{ |
22 |
RegistryKey key = Registry.LocalMachine; |
23 |
RegistryKey software = key.OpenSubKey("SOFTWARE"); |
24 |
RegistryKey DOFTECH = software.OpenSubKey("DOFTECH"); |
25 |
RegistryKey SPPIDConverter = DOFTECH.OpenSubKey("SPPIDConverter", true); |
26 |
path = SPPIDConverter.GetValue("path") + "licenses.licx"; |
27 |
if (File.Exists(path)) |
28 |
{ |
29 |
string data = File.ReadAllText(path); |
30 |
data = Crypt.decrypt(data, "dof1073#"); |
31 |
if (data.StartsWith("Date:")) |
32 |
{ |
33 |
data = data.Remove(0, "Date:".Length); |
34 |
|
35 |
if (string.IsNullOrEmpty(data)) |
36 |
{ |
37 |
if (SPPIDConverter.GetValue("Version") == null) |
38 |
{ |
39 |
SPPIDConverter.SetValue("Version", "U"); |
40 |
data = "Date:" + DateTime.Today.ToShortDateString(); |
41 |
data = Crypt.encrypt(data, "dof1073#"); |
42 |
File.WriteAllText(path, data); |
43 |
result = true; |
44 |
} |
45 |
} |
46 |
else |
47 |
{ |
48 |
DateTime dataTime = DateTime.MinValue; |
49 |
DateTime currentTime = DateTime.Now; |
50 |
if (DateTime.TryParse(data, out dataTime) && DateTime.Compare(dataTime.AddDays(14), currentTime) >= 0) |
51 |
result = true; |
52 |
|
53 |
} |
54 |
} |
55 |
else if (data.StartsWith("Absolute:")) |
56 |
{ |
57 |
data = data.Remove(0, "Absolute:".Length); |
58 |
string macAddress = NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString(); |
59 |
if (string.IsNullOrEmpty(data)) |
60 |
{ |
61 |
data = "Absolute:" + macAddress; |
62 |
data = Crypt.encrypt(data, "dof1073#"); |
63 |
File.WriteAllText(path, data); |
64 |
result = true; |
65 |
} |
66 |
else if (macAddress == data) |
67 |
result = true; |
68 |
} |
69 |
} |
70 |
} |
71 |
catch (Exception ex) |
72 |
{ |
73 |
|
74 |
} |
75 |
|
76 |
return result; |
77 |
} |
78 |
|
79 |
public static string SHA256Hash(string data) |
80 |
{ |
81 |
|
82 |
SHA256 sha = new SHA256Managed(); |
83 |
byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(data)); |
84 |
StringBuilder stringBuilder = new StringBuilder(); |
85 |
foreach (byte b in hash) |
86 |
{ |
87 |
stringBuilder.AppendFormat("{0:x2}", b); |
88 |
} |
89 |
return stringBuilder.ToString(); |
90 |
} |
91 |
|
92 |
private static void CreateDateLicenseFile() |
93 |
{ |
94 |
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Date]licenses.licx"; |
95 |
string data = "Date:"; |
96 |
using (StreamWriter sw = File.CreateText(path)) |
97 |
{ |
98 |
data = Crypt.encrypt(data, "dof1073#"); |
99 |
sw.Write(data); |
100 |
sw.Close(); |
101 |
sw.Dispose(); |
102 |
} |
103 |
} |
104 |
|
105 |
private static void CreateAbsoluteLicenseFile() |
106 |
{ |
107 |
string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Absolute]licenses.licx"; |
108 |
string data = "Absolute:"; |
109 |
using (StreamWriter sw = File.CreateText(path)) |
110 |
{ |
111 |
data = Crypt.encrypt(data, "dof1073#"); |
112 |
sw.Write(data); |
113 |
sw.Close(); |
114 |
sw.Dispose(); |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
class Crypt |
120 |
{ |
121 |
public static String encrypt(String text, String password) |
122 |
{ |
123 |
RijndaelManaged RijndaelCipher = new RijndaelManaged(); |
124 |
byte[] plainText = Encoding.UTF8.GetBytes(text); |
125 |
byte[] salt = Encoding.UTF8.GetBytes(password); |
126 |
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(password, salt); |
127 |
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)); |
128 |
MemoryStream memoryStream = new MemoryStream(); |
129 |
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write); |
130 |
cryptoStream.Write(plainText, 0, plainText.Length); // 암호화 시작 |
131 |
cryptoStream.FlushFinalBlock(); |
132 |
byte[] cryptBytes = memoryStream.ToArray(); |
133 |
// 스트림을 닫습니다. |
134 |
memoryStream.Close(); |
135 |
cryptoStream.Close(); |
136 |
String cryptResult = Convert.ToBase64String(cryptBytes); |
137 |
return cryptResult; // 암호화 문자열 리턴. |
138 |
} |
139 |
public static String decrypt(String text, String password) |
140 |
{ |
141 |
RijndaelManaged RijndaelCipher = new RijndaelManaged(); |
142 |
byte[] encryptData = Convert.FromBase64String(text); |
143 |
byte[] salt = Encoding.UTF8.GetBytes(password); |
144 |
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(password, salt); |
145 |
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); |
146 |
MemoryStream memoryStream = new MemoryStream(encryptData); |
147 |
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read); |
148 |
byte[] PlainText = new byte[encryptData.Length]; |
149 |
// 복호화 시작 |
150 |
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length); |
151 |
memoryStream.Close(); |
152 |
cryptoStream.Close(); |
153 |
// 복호화된 데이터를 문자열로 바꾼다. |
154 |
string DecryptedData = Encoding.UTF8.GetString(PlainText, 0, DecryptedCount); |
155 |
// 최종 결과 리턴 |
156 |
return DecryptedData; |
157 |
} |
158 |
} |
159 |
} |