Example #1
0
        public Encrypted(T obj, string secretKey, bool nullServerTime = false) : this()
        {
            Log.Debug("Encrypted object creating...");
            var serializer = SerializersFactory.GetSerializer <T>();

            Salt = SecureEncoding.CreateSalt();
            var iv = SecureEncoding.CreateIV(Salt, secretKey);

            using (var ms = new MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                ms.Position = 0;
                Data        = GetSecureEncoding(secretKey).Encrypt(ms.ToArray(), iv);
            }
            if (!nullServerTime)
            {
                ServerTime = DateTime.Now.ToUniversalTime();
            }
            Log.Debug("Encrypted object created");
        }
Example #2
0
 public T Decrypt(string secretKey)
 {
     Log.Debug("Decryption begin...");
     try
     {
         var iv             = SecureEncoding.CreateIV(Salt, secretKey);
         var secureEncoding = GetSecureEncoding(secretKey);
         var serializer     = SerializersFactory.GetSerializer <T>();
         var decryptedRaw   = secureEncoding.DecryptBytes(Data, iv);
         using (var ms = new MemoryStream(decryptedRaw))
             return((T)serializer.ReadObject(ms));
     }
     catch (Exception e)
     {
         Log.WarnFormat(e, "Ошибка при расшифровке строки");
         throw new DecryptException(e);
     }
     finally
     {
         Log.Debug("Decryption end...");
     }
 }