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