markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / Encrypt.cs @ 218e5002
이력 | 보기 | 이력해설 | 다운로드 (2.99 KB)
1 | 53c9637d | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.IO; |
||
4 | using System.Linq; |
||
5 | using System.Security.Cryptography; |
||
6 | using System.Text; |
||
7 | using System.Threading.Tasks; |
||
8 | |||
9 | namespace Markus.Service.Extensions |
||
10 | { |
||
11 | public class Encrypt |
||
12 | { |
||
13 | public static class AESEncrypter |
||
14 | { |
||
15 | private static RijndaelManaged _rManaged = new RijndaelManaged(); |
||
16 | private static UTF8Encoding _utf8Encoder = new UTF8Encoding(); |
||
17 | |||
18 | static AESEncrypter() |
||
19 | { |
||
20 | MD5 _md5 = new MD5CryptoServiceProvider(); |
||
21 | |||
22 | _rManaged.KeySize = 256; |
||
23 | _rManaged.BlockSize = 128; |
||
24 | _rManaged.Mode = CipherMode.CBC; |
||
25 | _rManaged.Padding = PaddingMode.PKCS7; |
||
26 | _rManaged.Key = _md5.ComputeHash(_utf8Encoder.GetBytes("zxcvbnmasdfghjklqwertyuiop098765")); |
||
27 | _rManaged.IV = _md5.ComputeHash(_utf8Encoder.GetBytes("qwertyuiopasdfghjklzxcvbnm123456")); |
||
28 | } |
||
29 | /// <summary>암호화</summary> |
||
30 | /// <param name="val">암호화할 값</param> |
||
31 | /// <returns>암호화된 값</returns> |
||
32 | public static string Encrypt(string val) |
||
33 | { |
||
34 | string _resVal = string.Empty; |
||
35 | byte[] _utf8Val = null; |
||
36 | byte[] _encryptVal = null; |
||
37 | ICryptoTransform tForm = _rManaged.CreateEncryptor(); |
||
38 | |||
39 | try |
||
40 | { |
||
41 | if (!string.IsNullOrEmpty(val)) |
||
42 | { |
||
43 | _utf8Val = _utf8Encoder.GetBytes(val); |
||
44 | _encryptVal = tForm.TransformFinalBlock(_utf8Val, 0, _utf8Val.Length); |
||
45 | _resVal = Convert.ToBase64String(_encryptVal); |
||
46 | } |
||
47 | else |
||
48 | _resVal = string.Empty; |
||
49 | } |
||
50 | catch |
||
51 | { |
||
52 | _resVal = string.Empty; |
||
53 | } |
||
54 | |||
55 | return _resVal; |
||
56 | } |
||
57 | /// <summary>복호화</summary> |
||
58 | /// <param name="val">복호화할 값</param> |
||
59 | /// <returns>복호화된 값</returns> |
||
60 | public static string Decrypt(string val) |
||
61 | { |
||
62 | string _resVal = string.Empty; |
||
63 | byte[] _decryptVal = null; |
||
64 | byte[] _encryptVal = null; |
||
65 | ICryptoTransform tForm = _rManaged.CreateDecryptor(); |
||
66 | |||
67 | try |
||
68 | { |
||
69 | if (!string.IsNullOrEmpty(val)) |
||
70 | { |
||
71 | _encryptVal = Convert.FromBase64String(val); |
||
72 | _decryptVal = tForm.TransformFinalBlock(_encryptVal, 0, _encryptVal.Length); |
||
73 | _resVal = _utf8Encoder.GetString(_decryptVal); |
||
74 | } |
||
75 | else |
||
76 | _resVal = string.Empty; |
||
77 | } |
||
78 | catch |
||
79 | { |
||
80 | _resVal = string.Empty; |
||
81 | } |
||
82 | |||
83 | return _resVal; |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |