markus / CommonLib / Common.cs @ 84c48033
이력 | 보기 | 이력해설 | 다운로드 (4.57 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.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 |
/// 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 |
public static string GetAlertMessageString(string section, string key, string def) |
67 |
{ |
68 |
System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(1024); |
69 |
GetPrivateProfileString(section, key, def, strbuilder, 1024, Path.Combine(AppDataFolder, "MARKUS.ini")); |
70 |
byte[] byte64 = Convert.FromBase64String(strbuilder.ToString()); |
71 |
|
72 |
return Encoding.UTF8.GetString(byte64); |
73 |
} |
74 |
/// <summary> |
75 |
/// 서버에 설치된 Service ini 의 Connection String 을 참조 |
76 |
/// </summary> |
77 |
/// <returns></returns> |
78 |
public static string GetConnectionString() |
79 |
{ |
80 |
System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512); |
81 |
GetPrivateProfileString("ConnectionString", "STRING", "", strbuilder, 512, Path.Combine(AppDataFolder, "FinalService.ini")); |
82 |
return Decrypt(strbuilder.ToString(), "Doftech1073#"); |
83 |
} |
84 |
private static string Decrypt(string textToDecrypt, string key) |
85 |
{ |
86 |
RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
87 |
rijndaelCipher.Mode = CipherMode.CBC; |
88 |
rijndaelCipher.Padding = PaddingMode.PKCS7; |
89 |
rijndaelCipher.KeySize = 128; |
90 |
rijndaelCipher.BlockSize = 128; |
91 |
|
92 |
byte[] encryptedData = Convert.FromBase64String(textToDecrypt); |
93 |
byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
94 |
byte[] keyBytes = new byte[16]; |
95 |
|
96 |
int len = pwdBytes.Length; |
97 |
if (len > keyBytes.Length) |
98 |
{ |
99 |
len = keyBytes.Length; |
100 |
} |
101 |
|
102 |
Array.Copy(pwdBytes, keyBytes, len); |
103 |
rijndaelCipher.Key = keyBytes; |
104 |
rijndaelCipher.IV = keyBytes; |
105 |
|
106 |
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); |
107 |
return Encoding.UTF8.GetString(plainText); |
108 |
} |
109 |
} |
110 |
} |