markus / FinalService / KCOM_FinalService / ConnectionStrEncrypt / Program.cs @ cbcc1a75
이력 | 보기 | 이력해설 | 다운로드 (3.25 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 |
Restart: |
15 |
|
16 |
string connection; |
17 |
string value; |
18 |
string key = "Doftech1073#"; |
19 |
|
20 |
Console.WriteLine("ex) data source=address;database=database;user id=userid;password=password"); |
21 |
Console.Write("input Connection String : "); |
22 |
|
23 |
connection = Console.ReadLine(); |
24 |
|
25 |
|
26 |
SelectEncrypt: |
27 |
|
28 |
Console.WriteLine("1.Encrypt 2.Decrypt"); |
29 |
Console.Write("Input 1 or 2 :"); |
30 |
|
31 |
value = Console.ReadLine().Trim(); |
32 |
|
33 |
if(value != "1" && value != "2") |
34 |
{ |
35 |
Console.WriteLine("Try again"); |
36 |
goto SelectEncrypt; |
37 |
} |
38 |
else if(value == "1") |
39 |
{ |
40 |
Console.WriteLine("Encrypt Connection String -"); |
41 |
Console.WriteLine(Encrypt(connection, key)); |
42 |
} |
43 |
else |
44 |
{ |
45 |
Console.WriteLine("Decrypt Connection String -"); |
46 |
Console.WriteLine(Decrypt(connection, key)); |
47 |
} |
48 |
|
49 |
goto Restart; |
50 |
} |
51 |
|
52 |
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 |
} |