프로젝트

일반

사용자정보

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

markus / CommonLib / Common.cs @ cdfb57ff

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

1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Linq;
5
using System.Runtime.InteropServices;
6
using System.Security.Cryptography;
7
using System.Text;
8
using System.Text.RegularExpressions;
9
using System.Threading.Tasks;
10

    
11
namespace CommonLib
12
{
13
    public class Common
14
    {
15
        [DllImport("kernel32")]
16
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
17

    
18
        [DllImport("kernel32")]
19
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
20

    
21
        public static string AppDataFolder
22
        {
23
            get
24
            {
25
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MARKUS");
26
            }
27
        }
28

    
29
        /// <summary>
30
        /// Client 에 설치된 MARKUS.ini 를 참조.
31
        /// </summary>
32
        /// <param name="section"></param>
33
        /// <param name="key"></param>
34
        /// <param name="def"></param>
35
        /// <returns></returns>
36
        public static string GetConfigString(string section, string key, string def)
37
        {
38
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
39
            GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
40
            return strbuilder.ToString();
41
        }
42

    
43
        /// <summary>
44
        /// Client 에 설치된 MARKUS.ini 를 참조.
45
        /// </summary>
46
        /// <param name="section"></param>
47
        /// <param name="key"></param>
48
        /// <param name="def"></param>
49
        /// <returns></returns>
50
        public static void WriteConfigString(string section, string key, string value)
51
        {
52
            WritePrivateProfileString(section, key, value, Path.Combine(AppDataFolder, "MARKUS.ini"));            
53
        }
54

    
55
        /// <summary>
56
        /// isExternal 이 True 이면 Internal IP를 External 로 치환하여 Return
57
        /// </summary>
58
        /// <param name="section"></param>
59
        /// <param name="key"></param>
60
        /// <param name="def"></param>
61
        /// <param name="isExternal"></param>
62
        /// <returns></returns>
63
        public static string GetConfigString(string section, string key, string def,bool isExternal)
64
        {
65
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
66
            GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
67

    
68
            string result = strbuilder.ToString();
69
            //internal 이면 result return
70
            if (isExternal)
71
            {
72
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512);
73
                GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
74
                string external_ip = stringBuilder.ToString();
75
                stringBuilder = new StringBuilder();
76
                GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
77
                string internal_ip = stringBuilder.ToString();
78
                result = result.Replace(internal_ip, external_ip);
79
            }
80
            return result;
81
        }
82
        public static string IPReplace(string url, bool isExternal)
83
        {
84
            string result = url;
85
            try
86
            {
87
                if (isExternal)
88
                {
89
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512);
90
                    GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
91
                    string external_ip = stringBuilder.ToString();
92
                    stringBuilder = new StringBuilder();
93
                    GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
94
                    string internal_ip = stringBuilder.ToString();
95
                    result = result.Replace(internal_ip, external_ip);
96
                }
97
            }
98
            catch (Exception)
99
            {
100
                throw;
101
            }
102
            
103
            return result;
104
        }
105
        public static string GetAlertMessageString(string section, string key, string def)
106
        {
107
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(1024);
108
            GetPrivateProfileString(section, key, def, strbuilder, 1024, Path.Combine(AppDataFolder, "MARKUS.ini"));
109
            byte[] byte64 = Convert.FromBase64String(strbuilder.ToString());
110
            
111
            return Regex.Unescape(Encoding.UTF8.GetString(byte64));
112
        }
113
        /// <summary>
114
        /// 서버에 설치된 Service ini 의 Connection String 을 참조
115
        /// </summary>
116
        /// <returns></returns>
117
        public static string GetConnectionString()
118
        {
119
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
120
            GetPrivateProfileString("ConnectionString", "STRING", "", strbuilder, 512, Path.Combine(AppDataFolder, "FinalService.ini"));
121
            return Decrypt(strbuilder.ToString(), "Doftech1073#");
122
        }
123
        private static string Decrypt(string textToDecrypt, string key)
124
        {
125
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
126
            rijndaelCipher.Mode = CipherMode.CBC;
127
            rijndaelCipher.Padding = PaddingMode.PKCS7;
128
            rijndaelCipher.KeySize = 128;
129
            rijndaelCipher.BlockSize = 128;
130

    
131
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
132
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
133
            byte[] keyBytes = new byte[16];
134

    
135
            int len = pwdBytes.Length;
136
            if (len > keyBytes.Length)
137
            {
138
                len = keyBytes.Length;
139
            }
140

    
141
            Array.Copy(pwdBytes, keyBytes, len);
142
            rijndaelCipher.Key = keyBytes;
143
            rijndaelCipher.IV = keyBytes;
144

    
145
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
146
            return Encoding.UTF8.GetString(plainText);
147
        }
148
    }
149
}
클립보드 이미지 추가 (최대 크기: 500 MB)