markus / FinalService / KCOM_FinalService / ConnectionStrEncrypt / Program.cs @ 8e6884a5
이력 | 보기 | 이력해설 | 다운로드 (3.05 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 |
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 |
String key = "Doftech1073#"; |
20 |
|
21 |
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 |
|
36 |
Console.ReadLine(); |
37 |
} |
38 |
|
39 |
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 |
} |