프로젝트

일반

사용자정보

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

markus / FinalService / KCOM_FinalService / ConnectionStrEncrypt / Program.cs @ 058a4c0d

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Security.Cryptography;
5
using System.Text;
6
using System.Threading.Tasks;
7

    
8
namespace ConnectionStrEncrypt
9
{
10
    class Program
11
    {
12
        static void Main(string[] args)
13
        {
14
            //args = new string[2];
15
            //args[0] = "-en";
16

    
17
            //Console.WriteLine("ex) data source=address;database=database;user id=userid;password=password");
18
            //Console.WriteLine("input Connection String :");
19

    
20
            //args[1] = Console.ReadLine();
21

    
22
            //args = new string[] { "-de", "oGKAXlIgXfRDRXGeHEn8EOYJBpEZdaR4TUqxSnjQeRQGAic7nOAFD0BzZ27j0u9NdhivuTsrcd+GXxxu0YVfH88OBZjEnWwEOO58gSOvKyw=" };
23

    
24
            if (args == null || args?.Count() == 0)
25
            {
26
                args = new[] { "-en", "data source=211.38.140.182;database=markus;user id=doftech;password=etecenc09!$" };
27
                //args = new[] { "-en", "data source=210.94.128.101;database=markus;user id=doftech;password=xpdhtm!1" };
28
                //args = new[] { "-en", "data source=10.11.142.77;database=markus;user id=doftech;password=dof1073#" };
29
            }
30

    
31
            String key = "Doftech1073#";
32

    
33
            if ((args.Length == 2) && (args[0] == "-en"))
34
            {
35
                String en = Encrypt(args[1], key);
36
                Console.WriteLine("Encrypted Text is \"" + en + "\"");
37
            }
38
            else if ((args.Length == 2) && (args[0] == "-de"))
39
            {
40
                String de = Decrypt(args[1], key);
41
                Console.WriteLine("Decrypted Text is \"" + de + "\"");
42
            }
43
            else
44
            {
45
                Console.WriteLine("Usage : ConnectionStrEncrypt.exe -en|-de <input string>");
46
            }
47

    
48
            Console.ReadLine();
49
        }
50

    
51
        public static string Decrypt(string textToDecrypt, string key)
52
        {
53
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
54
            rijndaelCipher.Mode = CipherMode.CBC;
55
            rijndaelCipher.Padding = PaddingMode.PKCS7;
56
            rijndaelCipher.KeySize = 128;
57
            rijndaelCipher.BlockSize = 128;
58

    
59
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
60
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
61
            byte[] keyBytes = new byte[16];
62

    
63
            int len = pwdBytes.Length;
64
            if (len > keyBytes.Length)
65
            {
66
                len = keyBytes.Length;
67
            }
68

    
69
            Array.Copy(pwdBytes, keyBytes, len);
70
            rijndaelCipher.Key = keyBytes;
71
            rijndaelCipher.IV = keyBytes;
72

    
73
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
74
            return Encoding.UTF8.GetString(plainText);
75
        }
76

    
77

    
78

    
79
        public static string Encrypt(string textToEncrypt, string key)
80
        {
81
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
82
            rijndaelCipher.Mode = CipherMode.CBC;
83
            rijndaelCipher.Padding = PaddingMode.PKCS7;
84
            rijndaelCipher.KeySize = 128;
85
            rijndaelCipher.BlockSize = 128;
86

    
87
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
88
            byte[] keyBytes = new byte[16];
89
            int len = pwdBytes.Length;
90
            if (len > keyBytes.Length)
91
            {
92
                len = keyBytes.Length;
93
            }
94

    
95
            Array.Copy(pwdBytes, keyBytes, len);
96
            rijndaelCipher.Key = keyBytes;
97
            rijndaelCipher.IV = keyBytes;
98
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
99
            byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt);
100
            return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
101
        }
102
    }
103
}
클립보드 이미지 추가 (최대 크기: 500 MB)