프로젝트

일반

사용자정보

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

markus / CommonLib / Common.cs @ 0ac88c70

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

1 de4c7a4a djkim
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.Threading.Tasks;
9
10
namespace CommonLib
11
{
12
    public class Common
13
    {
14
        [DllImport("kernel32")]
15
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
16
17 d0eda156 ljiyeon
        [DllImport("kernel32")]
18
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
19
20 de4c7a4a djkim
        public static string AppDataFolder
21
        {
22
            get
23
            {
24
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MARKUS");
25
            }
26
        }
27
28
        /// <summary>
29
        /// Client 에 설치된 MARKUS.ini 를 참조.
30
        /// </summary>
31
        /// <param name="section"></param>
32
        /// <param name="key"></param>
33
        /// <param name="def"></param>
34
        /// <returns></returns>
35
        public static string GetConfigString(string section, string key, string def)
36
        {
37
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
38
            GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
39
            return strbuilder.ToString();
40
        }
41
42
        /// <summary>
43 d0eda156 ljiyeon
        /// Client 에 설치된 MARKUS.ini 를 참조.
44
        /// </summary>
45
        /// <param name="section"></param>
46
        /// <param name="key"></param>
47
        /// <param name="def"></param>
48
        /// <returns></returns>
49
        public static void WriteConfigString(string section, string key, string value)
50
        {
51
            WritePrivateProfileString(section, key, value, Path.Combine(AppDataFolder, "MARKUS.ini"));            
52
        }
53
54
        /// <summary>
55 84c48033 djkim
        /// isExternal 이 True 이면 Internal IP를 External 로 치환하여 Return
56
        /// </summary>
57
        /// <param name="section"></param>
58
        /// <param name="key"></param>
59
        /// <param name="def"></param>
60
        /// <param name="isExternal"></param>
61
        /// <returns></returns>
62
        public static string GetConfigString(string section, string key, string def,bool isExternal)
63
        {
64
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
65
            GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
66
67
            string result = strbuilder.ToString();
68
            //internal 이면 result return
69
            if (isExternal)
70
            {
71
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512);
72
                GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
73
                string external_ip = stringBuilder.ToString();
74
                stringBuilder = new StringBuilder();
75
                GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
76
                string internal_ip = stringBuilder.ToString();
77
                result = result.Replace(internal_ip, external_ip);
78
            }
79
            return result;
80
        }
81 19391ad3 djkim
        public static string IPReplace(string url, bool isExternal)
82
        {
83
            string result = url;
84
            try
85
            {
86
                if (isExternal)
87
                {
88
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(512);
89
                    GetPrivateProfileString("External", "IP", "", stringBuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
90
                    string external_ip = stringBuilder.ToString();
91
                    stringBuilder = new StringBuilder();
92
                    GetPrivateProfileString("Internal", "IP", "", stringBuilder, 512, Path.Combine(AppDataFolder, "MARKUS.ini"));
93
                    string internal_ip = stringBuilder.ToString();
94
                    result = result.Replace(internal_ip, external_ip);
95
                }
96
            }
97
            catch (Exception)
98
            {
99
                throw;
100
            }
101
            
102
            return result;
103
        }
104 84c48033 djkim
        public static string GetAlertMessageString(string section, string key, string def)
105
        {
106
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(1024);
107
            GetPrivateProfileString(section, key, def, strbuilder, 1024, Path.Combine(AppDataFolder, "MARKUS.ini"));
108
            byte[] byte64 = Convert.FromBase64String(strbuilder.ToString());
109
            
110
            return Encoding.UTF8.GetString(byte64);
111
        }
112
        /// <summary>
113 de4c7a4a djkim
        /// 서버에 설치된 Service ini 의 Connection String 을 참조
114
        /// </summary>
115
        /// <returns></returns>
116
        public static string GetConnectionString()
117
        {
118
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512);
119
            GetPrivateProfileString("ConnectionString", "STRING", "", strbuilder, 512, Path.Combine(AppDataFolder, "FinalService.ini"));
120
            return Decrypt(strbuilder.ToString(), "Doftech1073#");
121
        }
122
        private static string Decrypt(string textToDecrypt, string key)
123
        {
124
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
125
            rijndaelCipher.Mode = CipherMode.CBC;
126
            rijndaelCipher.Padding = PaddingMode.PKCS7;
127
            rijndaelCipher.KeySize = 128;
128
            rijndaelCipher.BlockSize = 128;
129
130
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
131
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
132
            byte[] keyBytes = new byte[16];
133
134
            int len = pwdBytes.Length;
135
            if (len > keyBytes.Length)
136
            {
137
                len = keyBytes.Length;
138
            }
139
140
            Array.Copy(pwdBytes, keyBytes, len);
141
            rijndaelCipher.Key = keyBytes;
142
            rijndaelCipher.IV = keyBytes;
143
144
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
145
            return Encoding.UTF8.GetString(plainText);
146
        }
147
    }
148
}
클립보드 이미지 추가 (최대 크기: 500 MB)