markus / FinalService / KCOM_FinalService / ConnectionStrEncrypt / Program.cs @ cbcc1a75
이력 | 보기 | 이력해설 | 다운로드 (3.25 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 | cbcc1a75 | taeseongkim | Restart: |
15 | 77cdac33 | taeseongkim | |
16 | cbcc1a75 | taeseongkim | string connection; |
17 | string value; |
||
18 | string key = "Doftech1073#"; |
||
19 | 77cdac33 | taeseongkim | |
20 | cbcc1a75 | taeseongkim | Console.WriteLine("ex) data source=address;database=database;user id=userid;password=password"); |
21 | Console.Write("input Connection String : "); |
||
22 | 77cdac33 | taeseongkim | |
23 | cbcc1a75 | taeseongkim | connection = Console.ReadLine(); |
24 | 77cdac33 | taeseongkim | |
25 | e1c892f7 | taeseongkim | |
26 | cbcc1a75 | taeseongkim | SelectEncrypt: |
27 | |||
28 | Console.WriteLine("1.Encrypt 2.Decrypt"); |
||
29 | Console.Write("Input 1 or 2 :"); |
||
30 | |||
31 | value = Console.ReadLine().Trim(); |
||
32 | e05bed4e | djkim | |
33 | cbcc1a75 | taeseongkim | if(value != "1" && value != "2") |
34 | d1be84a0 | humkyung | { |
35 | cbcc1a75 | taeseongkim | Console.WriteLine("Try again"); |
36 | goto SelectEncrypt; |
||
37 | d1be84a0 | humkyung | } |
38 | cbcc1a75 | taeseongkim | else if(value == "1") |
39 | d1be84a0 | humkyung | { |
40 | cbcc1a75 | taeseongkim | Console.WriteLine("Encrypt Connection String -"); |
41 | Console.WriteLine(Encrypt(connection, key)); |
||
42 | d1be84a0 | humkyung | } |
43 | else |
||
44 | { |
||
45 | cbcc1a75 | taeseongkim | Console.WriteLine("Decrypt Connection String -"); |
46 | Console.WriteLine(Decrypt(connection, key)); |
||
47 | d1be84a0 | humkyung | } |
48 | e1c892f7 | taeseongkim | |
49 | cbcc1a75 | taeseongkim | goto Restart; |
50 | e05bed4e | djkim | } |
51 | d1be84a0 | humkyung | |
52 | e05bed4e | djkim | public static string Decrypt(string textToDecrypt, string key) |
53 | { |
||
54 | RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
||
55 | rijndaelCipher.Mode = CipherMode.CBC; |
||
56 | rijndaelCipher.Padding = PaddingMode.PKCS7; |
||
57 | rijndaelCipher.KeySize = 128; |
||
58 | rijndaelCipher.BlockSize = 128; |
||
59 | |||
60 | byte[] encryptedData = Convert.FromBase64String(textToDecrypt); |
||
61 | byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
||
62 | byte[] keyBytes = new byte[16]; |
||
63 | |||
64 | int len = pwdBytes.Length; |
||
65 | if (len > keyBytes.Length) |
||
66 | { |
||
67 | len = keyBytes.Length; |
||
68 | } |
||
69 | |||
70 | Array.Copy(pwdBytes, keyBytes, len); |
||
71 | rijndaelCipher.Key = keyBytes; |
||
72 | rijndaelCipher.IV = keyBytes; |
||
73 | |||
74 | byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); |
||
75 | return Encoding.UTF8.GetString(plainText); |
||
76 | } |
||
77 | |||
78 | |||
79 | |||
80 | public static string Encrypt(string textToEncrypt, string key) |
||
81 | { |
||
82 | RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
||
83 | rijndaelCipher.Mode = CipherMode.CBC; |
||
84 | rijndaelCipher.Padding = PaddingMode.PKCS7; |
||
85 | rijndaelCipher.KeySize = 128; |
||
86 | rijndaelCipher.BlockSize = 128; |
||
87 | |||
88 | byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
||
89 | byte[] keyBytes = new byte[16]; |
||
90 | int len = pwdBytes.Length; |
||
91 | if (len > keyBytes.Length) |
||
92 | { |
||
93 | len = keyBytes.Length; |
||
94 | } |
||
95 | |||
96 | Array.Copy(pwdBytes, keyBytes, len); |
||
97 | rijndaelCipher.Key = keyBytes; |
||
98 | rijndaelCipher.IV = keyBytes; |
||
99 | ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); |
||
100 | byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt); |
||
101 | return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); |
||
102 | } |
||
103 | } |
||
104 | } |