프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / License.cs @ a03e9271

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

    
62
            }
63

    
64
            return result;
65
        }
66

    
67
        public static string SHA256Hash(string data)
68
        {
69

    
70
            SHA256 sha = new SHA256Managed();
71
            byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(data));
72
            StringBuilder stringBuilder = new StringBuilder();
73
            foreach (byte b in hash)
74
            {
75
                stringBuilder.AppendFormat("{0:x2}", b);
76
            }
77
            return stringBuilder.ToString();
78
        }
79

    
80
        private static void CreateDateLicenseFile()
81
        {
82
            string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Date]licenses.licx";
83
            string data = "Date:";
84
            using (StreamWriter sw = File.CreateText(path))
85
            {
86
                data = Crypt.encrypt(data, "dof1073#");
87
                sw.Write(data);
88
                sw.Close();
89
                sw.Dispose();
90
            }
91
        }
92

    
93
        private static void CreateAbsoluteLicenseFile()
94
        {
95
            string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\[Absolute]licenses.licx";
96
            string data = "Absolute:";
97
            using (StreamWriter sw = File.CreateText(path))
98
            {
99
                data = Crypt.encrypt(data, "dof1073#");
100
                sw.Write(data);
101
                sw.Close();
102
                sw.Dispose();
103
            }
104
        }
105
    }
106

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