프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / SPPIDConverter / License.cs @ 91c7a82a

이력 | 보기 | 이력해설 | 다운로드 (6.22 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");
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
                            data = "Date:" + DateTime.Today.ToShortDateString();
38
                            data = Crypt.encrypt(data, "dof1073#");
39
                            File.WriteAllText(path, data);
40
                            result = true;
41
                        }
42
                        else
43
                        {
44
                            DateTime dataTime = DateTime.MinValue;
45
                            DateTime currentTime = DateTime.Now;
46
                            if (DateTime.TryParse(data, out dataTime) && DateTime.Compare(dataTime.AddDays(7), currentTime) >= 0)
47
                                result = true;
48
                        }
49
                    }
50
                    else if (data.StartsWith("Absolute:"))
51
                    {
52
                        data = data.Remove(0, "Absolute:".Length);
53
                        string macAddress = NetworkInterface.GetAllNetworkInterfaces()[0].GetPhysicalAddress().ToString();
54
                        if (string.IsNullOrEmpty(data))
55
                        {
56
                            data = "Absolute:" + macAddress;
57
                            data = Crypt.encrypt(data, "dof1073#");
58
                            File.WriteAllText(path, data);
59
                            result = true;
60
                        }
61
                        else if (macAddress == data)
62
                            result = true;
63
                    }
64
                }
65
            }
66
            catch (Exception ex)
67
            {
68

    
69
            }
70

    
71
            return result;
72
        }
73

    
74
        public static string SHA256Hash(string data)
75
        {
76

    
77
            SHA256 sha = new SHA256Managed();
78
            byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(data));
79
            StringBuilder stringBuilder = new StringBuilder();
80
            foreach (byte b in hash)
81
            {
82
                stringBuilder.AppendFormat("{0:x2}", b);
83
            }
84
            return stringBuilder.ToString();
85
        }
86

    
87
        private static void CreateDateLicenseFile()
88
        {
89
            string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Date]licenses.licx";
90
            string data = "Date:";
91
            using (StreamWriter sw = File.CreateText(path))
92
            {
93
                data = Crypt.encrypt(data, "dof1073#");
94
                sw.Write(data);
95
                sw.Close();
96
                sw.Dispose();
97
            }
98
        }
99

    
100
        private static void CreateAbsoluteLicenseFile()
101
        {
102
            string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Absolute]licenses.licx";
103
            string data = "Absolute:";
104
            using (StreamWriter sw = File.CreateText(path))
105
            {
106
                data = Crypt.encrypt(data, "dof1073#");
107
                sw.Write(data);
108
                sw.Close();
109
                sw.Dispose();
110
            }
111
        }
112
    }
113

    
114
    class Crypt
115
    {
116
        public static String encrypt(String text, String password)
117
        {
118
            RijndaelManaged RijndaelCipher = new RijndaelManaged();
119
            byte[] plainText = Encoding.UTF8.GetBytes(text);
120
            byte[] salt = Encoding.UTF8.GetBytes(password);
121
            PasswordDeriveBytes secretKey = new PasswordDeriveBytes(password, salt);
122
            ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
123
            MemoryStream memoryStream = new MemoryStream();
124
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
125
            cryptoStream.Write(plainText, 0, plainText.Length);     // 암호화 시작
126
            cryptoStream.FlushFinalBlock();
127
            byte[] cryptBytes = memoryStream.ToArray();
128
            // 스트림을 닫습니다.
129
            memoryStream.Close();
130
            cryptoStream.Close();
131
            String cryptResult = Convert.ToBase64String(cryptBytes);
132
            return cryptResult;     // 암호화 문자열 리턴.
133
        }
134
        public static String decrypt(String text, String password)
135
        {
136
            RijndaelManaged RijndaelCipher = new RijndaelManaged();
137
            byte[] encryptData = Convert.FromBase64String(text);
138
            byte[] salt = Encoding.UTF8.GetBytes(password);
139
            PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(password, salt);
140
            ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
141
            MemoryStream memoryStream = new MemoryStream(encryptData);
142
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
143
            byte[] PlainText = new byte[encryptData.Length];
144
            // 복호화 시작
145
            int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
146
            memoryStream.Close();
147
            cryptoStream.Close();
148
            // 복호화된 데이터를 문자열로 바꾼다.
149
            string DecryptedData = Encoding.UTF8.GetString(PlainText, 0, DecryptedCount);
150
            // 최종 결과 리턴
151
            return DecryptedData;
152
        }
153
    }
154
}