프로젝트

일반

사용자정보

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

markus / CommonLib / Common.cs @ 508c3ac5

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