markus / FinalService / KCOM_FinalService / ConnectionStrEncrypt / Program.cs @ d1be84a0
이력 | 보기 | 이력해설 | 다운로드 (2.92 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 | d1be84a0 | humkyung | ///String originalText = "data source=cloud.devdoftech.co.kr,7777;database={0};user id=doftech;password=dof1073#"; |
15 | e05bed4e | djkim | String key = "Doftech1073#"; |
16 | |||
17 | d1be84a0 | humkyung | if ((args.Length == 2) && (args[0] == "-en")) |
18 | { |
||
19 | String en = Encrypt(args[1], key); |
||
20 | Console.WriteLine("Encrypted Text is \"" + en + "\""); |
||
21 | } |
||
22 | else if ((args.Length == 2) && (args[0] == "-de")) |
||
23 | { |
||
24 | String de = Decrypt(args[1], key); |
||
25 | Console.WriteLine("Decrypted Text is \"" + de + "\""); |
||
26 | } |
||
27 | else |
||
28 | { |
||
29 | Console.WriteLine("Usage : ConnectionStrEncrypt.exe -en|-de <input string>"); |
||
30 | } |
||
31 | e05bed4e | djkim | } |
32 | d1be84a0 | humkyung | |
33 | e05bed4e | djkim | public static string Decrypt(string textToDecrypt, string key) |
34 | { |
||
35 | RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
||
36 | rijndaelCipher.Mode = CipherMode.CBC; |
||
37 | rijndaelCipher.Padding = PaddingMode.PKCS7; |
||
38 | rijndaelCipher.KeySize = 128; |
||
39 | rijndaelCipher.BlockSize = 128; |
||
40 | |||
41 | byte[] encryptedData = Convert.FromBase64String(textToDecrypt); |
||
42 | byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
||
43 | byte[] keyBytes = new byte[16]; |
||
44 | |||
45 | int len = pwdBytes.Length; |
||
46 | if (len > keyBytes.Length) |
||
47 | { |
||
48 | len = keyBytes.Length; |
||
49 | } |
||
50 | |||
51 | Array.Copy(pwdBytes, keyBytes, len); |
||
52 | rijndaelCipher.Key = keyBytes; |
||
53 | rijndaelCipher.IV = keyBytes; |
||
54 | |||
55 | byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); |
||
56 | return Encoding.UTF8.GetString(plainText); |
||
57 | } |
||
58 | |||
59 | |||
60 | |||
61 | public static string Encrypt(string textToEncrypt, string key) |
||
62 | { |
||
63 | RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
||
64 | rijndaelCipher.Mode = CipherMode.CBC; |
||
65 | rijndaelCipher.Padding = PaddingMode.PKCS7; |
||
66 | rijndaelCipher.KeySize = 128; |
||
67 | rijndaelCipher.BlockSize = 128; |
||
68 | |||
69 | byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
||
70 | byte[] keyBytes = new byte[16]; |
||
71 | int len = pwdBytes.Length; |
||
72 | if (len > keyBytes.Length) |
||
73 | { |
||
74 | len = keyBytes.Length; |
||
75 | } |
||
76 | |||
77 | Array.Copy(pwdBytes, keyBytes, len); |
||
78 | rijndaelCipher.Key = keyBytes; |
||
79 | rijndaelCipher.IV = keyBytes; |
||
80 | ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); |
||
81 | byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt); |
||
82 | return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); |
||
83 | } |
||
84 | } |
||
85 | } |