markus / FinalService / FinalServiceBase / CommonLib / Common.cs @ 42d49521
이력 | 보기 | 이력해설 | 다운로드 (2.4 KB)
1 | 42d49521 | taeseongkim | 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 AppDomain.CurrentDomain.BaseDirectory; |
||
22 | //return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MARKUS"); |
||
23 | } |
||
24 | } |
||
25 | public static string GetConfigString(string section, string key, string def) |
||
26 | { |
||
27 | System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512); |
||
28 | GetPrivateProfileString(section, key, def, strbuilder, 512, Path.Combine(AppDataFolder, "FinalService.ini")); |
||
29 | return strbuilder.ToString(); |
||
30 | } |
||
31 | |||
32 | public static string GetConnectionString() |
||
33 | { |
||
34 | System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(512); |
||
35 | GetPrivateProfileString("ConnectionString", "STRING", "", strbuilder, 512, Path.Combine(AppDataFolder, "FinalService.ini")); |
||
36 | return Decrypt(strbuilder.ToString(), "Doftech1073#"); |
||
37 | } |
||
38 | private static string Decrypt(string textToDecrypt, string key) |
||
39 | { |
||
40 | RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
||
41 | rijndaelCipher.Mode = CipherMode.CBC; |
||
42 | rijndaelCipher.Padding = PaddingMode.PKCS7; |
||
43 | rijndaelCipher.KeySize = 128; |
||
44 | rijndaelCipher.BlockSize = 128; |
||
45 | |||
46 | byte[] encryptedData = Convert.FromBase64String(textToDecrypt); |
||
47 | byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
||
48 | byte[] keyBytes = new byte[16]; |
||
49 | |||
50 | int len = pwdBytes.Length; |
||
51 | if (len > keyBytes.Length) |
||
52 | { |
||
53 | len = keyBytes.Length; |
||
54 | } |
||
55 | |||
56 | Array.Copy(pwdBytes, keyBytes, len); |
||
57 | rijndaelCipher.Key = keyBytes; |
||
58 | rijndaelCipher.IV = keyBytes; |
||
59 | |||
60 | byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); |
||
61 | return Encoding.UTF8.GetString(plainText); |
||
62 | } |
||
63 | } |
||
64 | } |