Commits

HexRx committed ad389bb

Modified the account entity

Comments (0)

Files changed (1)

KPDataSave/AccountState.cs

 */
 
 using System;
+using System.IO;
+using System.Security.Cryptography;
+using System.Text;
 using KeePass.Plugins;
 using Newtonsoft.Json;
 
             // Т.к KeePass сохраняет настройки только при закрытии приложения, необходимо хранить копию в памяти
             _account = this;
             string json = JsonConvert.SerializeObject(this);
-            host.CustomConfig.SetString(Key, json);
+            var symmetricKey = new RijndaelManaged();
+            ICryptoTransform cryptoTransform = symmetricKey.CreateEncryptor(Encoding.UTF8.GetBytes("hhekhlyshoqxt5s"), 
+                Encoding.UTF8.GetBytes("7movlrmyr1txov5e"));
+            using (var memoryStream = new MemoryStream())
+            {
+                using (var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
+                {
+                    using (var streamWriter = new StreamWriter(cryptoStream))
+                    {
+                        streamWriter.Write(json);
+                    }
+                    host.CustomConfig.SetString(Key, Convert.ToBase64String(memoryStream.ToArray()));
+                }
+            }
         }
 
         public static AccountState Deserialize(IPluginHost host)
             if (_account != null)
                 return _account;
             string value = host.CustomConfig.GetString(Key);
+            if (String.IsNullOrEmpty(value))
+               return null;
+            var symmetricKey = new RijndaelManaged();
+            ICryptoTransform cryptoTransform = symmetricKey.CreateDecryptor(Encoding.UTF8.GetBytes("hhekhlyshoqxt5s"), 
+                Encoding.UTF8.GetBytes("7movlrmyr1txov5e"));
+            string dvalue;
+            using (var memoryStream = new MemoryStream(Convert.FromBase64String(value)))
+            {
+                using (var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Read))
+                {
+                    using (var streamReader = new StreamReader(cryptoStream))
+                    {
+                        dvalue = streamReader.ReadToEnd();
+                    }
+                }
+            }
             try
             {
-                _account = JsonConvert.DeserializeObject<AccountState>(value);
+                _account = JsonConvert.DeserializeObject<AccountState>(dvalue);
             }
             // В конфиге пусто
             catch (ArgumentNullException)