프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / FinalService / KCOM_FinalService / ConnectionStrEncrypt / Program.cs @ e05bed4e

이력 | 보기 | 이력해설 | 다운로드 (2.77 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
            //String originalText = "data source=ESB-DB;database={0};user id=ProjectPortalDBConn;password=ProjectPortalDBConn";
15
            String originalText = "data source=cloud.devdoftech.co.kr,7777;database={0};user id=doftech;password=dof1073#";
16
            String key = "Doftech1073#";
17

    
18
            String en = Encrypt(originalText, key);
19
            String de = Decrypt(en, key);
20

    
21
            Console.WriteLine("Original Text is " + originalText);
22
            Console.WriteLine("Encrypted Text is " + en);
23
            Console.WriteLine("Decrypted Text is " + de);
24

    
25
        }
26
        public static string Decrypt(string textToDecrypt, string key)
27
        {
28
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
29
            rijndaelCipher.Mode = CipherMode.CBC;
30
            rijndaelCipher.Padding = PaddingMode.PKCS7;
31
            rijndaelCipher.KeySize = 128;
32
            rijndaelCipher.BlockSize = 128;
33

    
34
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
35
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
36
            byte[] keyBytes = new byte[16];
37

    
38
            int len = pwdBytes.Length;
39
            if (len > keyBytes.Length)
40
            {
41
                len = keyBytes.Length;
42
            }
43

    
44
            Array.Copy(pwdBytes, keyBytes, len);
45
            rijndaelCipher.Key = keyBytes;
46
            rijndaelCipher.IV = keyBytes;
47

    
48
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
49
            return Encoding.UTF8.GetString(plainText);
50
        }
51

    
52

    
53

    
54
        public static string Encrypt(string textToEncrypt, string key)
55
        {
56
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
57
            rijndaelCipher.Mode = CipherMode.CBC;
58
            rijndaelCipher.Padding = PaddingMode.PKCS7;
59
            rijndaelCipher.KeySize = 128;
60
            rijndaelCipher.BlockSize = 128;
61

    
62
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
63
            byte[] keyBytes = new byte[16];
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
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
74
            byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt);
75
            return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
76
        }
77

    
78

    
79
    }
80
}
클립보드 이미지 추가 (최대 크기: 500 MB)