프로젝트

일반

사용자정보

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

markus / FinalService / KCOM_FinalService / ConnectionStrEncrypt / Program.cs @ 14f7c8c5

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

1 e05bed4e djkim
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 e1c892f7 taeseongkim
            if (args == null || args?.Count() == 0)
15
            {
16
                args = new[] { "-en", "data source=10.11.252.2,9875;database={0};user id=ProjectPortalDBConn;password=ProjectPortalDBConn" };
17
            }
18
19 e05bed4e djkim
            String key = "Doftech1073#";
20
21 d1be84a0 humkyung
            if ((args.Length == 2) && (args[0] == "-en"))
22
            {
23
                String en = Encrypt(args[1], key);
24
                Console.WriteLine("Encrypted Text is \"" + en + "\"");
25
            }
26
            else if ((args.Length == 2) && (args[0] == "-de"))
27
            {
28
                String de = Decrypt(args[1], key);
29
                Console.WriteLine("Decrypted Text is \"" + de + "\"");
30
            }
31
            else
32
            {
33
                Console.WriteLine("Usage : ConnectionStrEncrypt.exe -en|-de <input string>");
34
            }
35 e1c892f7 taeseongkim
36
            Console.ReadLine();
37 e05bed4e djkim
        }
38 d1be84a0 humkyung
39 e05bed4e djkim
        public 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
66
67
        public static string Encrypt(string textToEncrypt, string key)
68
        {
69
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
70
            rijndaelCipher.Mode = CipherMode.CBC;
71
            rijndaelCipher.Padding = PaddingMode.PKCS7;
72
            rijndaelCipher.KeySize = 128;
73
            rijndaelCipher.BlockSize = 128;
74
75
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
76
            byte[] keyBytes = new byte[16];
77
            int len = pwdBytes.Length;
78
            if (len > keyBytes.Length)
79
            {
80
                len = keyBytes.Length;
81
            }
82
83
            Array.Copy(pwdBytes, keyBytes, len);
84
            rijndaelCipher.Key = keyBytes;
85
            rijndaelCipher.IV = keyBytes;
86
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
87
            byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt);
88
            return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
89
        }
90
    }
91
}
클립보드 이미지 추가 (최대 크기: 500 MB)