프로젝트

일반

사용자정보

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

markus / FinalServiceV3 / KCOM_FinalService / CommonLib / Common.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (2.4 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 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

    
39
        private static string Decrypt(string textToDecrypt, string key)
40
        {
41
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
42
            rijndaelCipher.Mode = CipherMode.CBC;
43
            rijndaelCipher.Padding = PaddingMode.PKCS7;
44
            rijndaelCipher.KeySize = 128;
45
            rijndaelCipher.BlockSize = 128;
46

    
47
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
48
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
49
            byte[] keyBytes = new byte[16];
50

    
51
            int len = pwdBytes.Length;
52
            if (len > keyBytes.Length)
53
            {
54
                len = keyBytes.Length;
55
            }
56

    
57
            Array.Copy(pwdBytes, keyBytes, len);
58
            rijndaelCipher.Key = keyBytes;
59
            rijndaelCipher.IV = keyBytes;
60

    
61
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
62
            return Encoding.UTF8.GetString(plainText);
63
        }
64
    }
65
}
클립보드 이미지 추가 (최대 크기: 500 MB)