a4a1b252b6f6319628cbd877c91b49ab3abfd01e
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl.Implementations / Cryptography.cs
1 using System;
2 using System.IO;
3 using System.Security.Cryptography;
4 using System.Text;
5
6 namespace SpeedportHybridControl.Implementations
7 {
8 public static class Cryptography
9 {
10 private static string KEY = "8E16A57381AFDA47856682CEBE85DCF5982F59321AE28B2822C1C9E1FC481C50";
11 private static string IV = "7CD37E78623793D4C4BB81DB73B08522";
12
13 public static string Encrypt(string clearText)
14 {
15 byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
16 string result;
17 using (Aes encryptor = Aes.Create())
18 {
19 if (Object.Equals(encryptor, null))
20 {
21 result = null;
22 return result;
23 }
24
25 encryptor.KeySize = 256;
26 encryptor.BlockSize = 128;
27 encryptor.Mode = CipherMode.CBC;
28 encryptor.Padding = PaddingMode.PKCS7;
29 encryptor.Key = util.HexToByte(KEY);
30 encryptor.IV = util.HexToByte(IV);
31
32 using (MemoryStream ms = new MemoryStream())
33 {
34 using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
35 {
36 cs.Write(clearBytes, 0, clearBytes.Length);
37 cs.Close();
38 }
39 clearText = Convert.ToBase64String(ms.ToArray());
40 }
41 }
42 result = clearText;
43 return result;
44 }
45
46 public static string Decrypt(string cipherText)
47 {
48 byte[] cipherBytes = Convert.FromBase64String(cipherText);
49 string result;
50 using (Aes encryptor = Aes.Create())
51 {
52 if (Object.Equals(encryptor, null))
53 {
54 result = null;
55 return result;
56 }
57
58 encryptor.KeySize = 256;
59 encryptor.BlockSize = 128;
60 encryptor.Mode = CipherMode.CBC;
61 encryptor.Padding = PaddingMode.PKCS7;
62 encryptor.Key = util.HexToByte(KEY);
63 encryptor.IV = util.HexToByte(IV);
64
65 using (MemoryStream ms = new MemoryStream())
66 {
67 using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
68 {
69 cs.Write(cipherBytes, 0, cipherBytes.Length);
70 cs.Close();
71 }
72 cipherText = Encoding.Unicode.GetString(ms.ToArray());
73 }
74 }
75 result = cipherText;
76 return result;
77 }
78 }
79 }