markus / FinalService / KCOM_FinalService / ConnectionStrEncrypt / Program.cs @ a7bee7cf
이력 | 보기 | 이력해설 | 다운로드 (3.58 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=210.94.128.101;database=markus;user id=doftech;password=xpdhtm!1" }; |
27 |
//args = new[] { "-en", "data source=10.11.142.77;database=markus;user id=doftech;password=dof1073#" }; |
28 |
} |
29 |
|
30 |
String key = "Doftech1073#"; |
31 |
|
32 |
if ((args.Length == 2) && (args[0] == "-en")) |
33 |
{ |
34 |
String en = Encrypt(args[1], key); |
35 |
Console.WriteLine("Encrypted Text is \"" + en + "\""); |
36 |
} |
37 |
else if ((args.Length == 2) && (args[0] == "-de")) |
38 |
{ |
39 |
String de = Decrypt(args[1], key); |
40 |
Console.WriteLine("Decrypted Text is \"" + de + "\""); |
41 |
} |
42 |
else |
43 |
{ |
44 |
Console.WriteLine("Usage : ConnectionStrEncrypt.exe -en|-de <input string>"); |
45 |
} |
46 |
|
47 |
Console.ReadLine(); |
48 |
} |
49 |
|
50 |
public static string Decrypt(string textToDecrypt, string key) |
51 |
{ |
52 |
RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
53 |
rijndaelCipher.Mode = CipherMode.CBC; |
54 |
rijndaelCipher.Padding = PaddingMode.PKCS7; |
55 |
rijndaelCipher.KeySize = 128; |
56 |
rijndaelCipher.BlockSize = 128; |
57 |
|
58 |
byte[] encryptedData = Convert.FromBase64String(textToDecrypt); |
59 |
byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
60 |
byte[] keyBytes = new byte[16]; |
61 |
|
62 |
int len = pwdBytes.Length; |
63 |
if (len > keyBytes.Length) |
64 |
{ |
65 |
len = keyBytes.Length; |
66 |
} |
67 |
|
68 |
Array.Copy(pwdBytes, keyBytes, len); |
69 |
rijndaelCipher.Key = keyBytes; |
70 |
rijndaelCipher.IV = keyBytes; |
71 |
|
72 |
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length); |
73 |
return Encoding.UTF8.GetString(plainText); |
74 |
} |
75 |
|
76 |
|
77 |
|
78 |
public static string Encrypt(string textToEncrypt, string key) |
79 |
{ |
80 |
RijndaelManaged rijndaelCipher = new RijndaelManaged(); |
81 |
rijndaelCipher.Mode = CipherMode.CBC; |
82 |
rijndaelCipher.Padding = PaddingMode.PKCS7; |
83 |
rijndaelCipher.KeySize = 128; |
84 |
rijndaelCipher.BlockSize = 128; |
85 |
|
86 |
byte[] pwdBytes = Encoding.UTF8.GetBytes(key); |
87 |
byte[] keyBytes = new byte[16]; |
88 |
int len = pwdBytes.Length; |
89 |
if (len > keyBytes.Length) |
90 |
{ |
91 |
len = keyBytes.Length; |
92 |
} |
93 |
|
94 |
Array.Copy(pwdBytes, keyBytes, len); |
95 |
rijndaelCipher.Key = keyBytes; |
96 |
rijndaelCipher.IV = keyBytes; |
97 |
ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); |
98 |
byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt); |
99 |
return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); |
100 |
} |
101 |
} |
102 |
} |