· 7 years ago · Dec 10, 2018, 11:38 PM
1 private static IDictionary<byte, SymmetricKey> SymmetricKeys
2{
3 get
4 {
5 if (symmetricKeys == null)
6 {
7 lock (typeof(SecurityManager))
8 {
9 if (symmetricKeys == null)
10 {
11 try
12 {
13 IList<SymmetricKey> result = GetSymmetricKeys();
14 symmetricKeys = new Dictionary<byte, SymmetricKey>(result.Count);
15 SymmetricKey lastActiveKey = null;
16 foreach (SymmetricKey key in result)
17 {
18 symmetricKeys.Add(key.KeyID, key);
19 if ((DateTime.Today <= key.DateExpired) && key.Active == true)
20 activeKey = key;
21 if (key.Active == true)
22 lastActiveKey = key;
23 }
24 if (activeKey == null)
25 {
26 LogManager.LogEvent(200, "Cryptography", ExceptionMessages.events_framework_AllKeysExpired, System.Diagnostics.EventLogEntryType.Warning);
27 if (lastActiveKey != null)
28 activeKey = lastActiveKey;
29 else
30 throw new CryptographicException(ExceptionMessages.exceptions_framework_NoActiveKey);
31 }
32 }
33 catch (CryptographicException)
34 {
35 throw;
36 }
37 catch (Exception ex)
38 {
39 throw new CryptographicException(ExceptionMessages.exceptions_framework_InitSymmetricKeysFailed, ex);
40 }
41 }
42 }
43 }
44 return symmetricKeys;
45 }
46}
47
48public static List<SymmetricKey> GetSymmetricKeys()
49{
50 string filePath = ConfigManager.KeyFilePath;
51
52 XmlDocument xmlDoc = new XmlDocument();
53 xmlDoc.Load(filePath);
54
55 List<SymmetricKey> keys = new List<SymmetricKey>();
56 foreach (XmlNode node in xmlDoc.FirstChild.ChildNodes)
57 {
58 SymmetricKey key = new SymmetricKey(Convert.ToByte(node.Attributes["KeyID"].Value),
59 Encoding.ASCII.GetString(DataEncryption.DecryptSymmetric(Convert.FromBase64String(node.Attributes["Key"].Value))),
60 Convert.ToBoolean(node.Attributes["Active"].Value),
61 Convert.ToDateTime(node.Attributes["DateActivated"].Value),
62 Convert.ToDateTime(node.Attributes["DateExpired"].Value),
63 Convert.ToDateTime(node.Attributes["DateCreated"].Value),
64 node.Attributes["UsernameCreated"].Value);
65 keys.Add(key);
66
67 }
68 return keys;
69}
70
71public static byte[] DecryptSymmetric(byte[] cipherText)
72{
73 return DecryptByteArray(cipherText, Cryptographer.MasterKey);
74}
75
76public static byte[] DecryptByteArray(byte[] EncryptedData, string Password)
77{
78 if (Password == null)
79 throw new ArgumentNullException("Password");
80 RijndaelManaged RijndaelCipher = new RijndaelManaged();
81 byte[] result;
82 try
83 {
84 byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString(CultureInfo.InvariantCulture));
85 //Making of the key for decryption
86 PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
87
88 //Creates a symmetric Rijndael decryptor object.
89 ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
90 MemoryStream memoryStream = new MemoryStream(EncryptedData);
91 //Defines the cryptographics stream for decryption.THe stream contains decrpted data
92 CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
93 byte[] PlainText = new byte[EncryptedData.Length];
94 int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
95 memoryStream.Close();
96 cryptoStream.Close();
97 //Converting to string
98 result = new byte[DecryptedCount];
99 Array.Copy(PlainText, result, DecryptedCount);
100
101 }
102 finally
103 {
104 RijndaelCipher.Clear();
105 }
106 return result;
107
108}