Esempio n. 1
0
        private string Encrypt(string plainText, string password)
        {
            byte[] salt       = Encoding.Unicode.GetBytes(Salt);
            byte[] plainBytes = Encoding.Unicode.GetBytes(Compressed ? CompressionUtility.GZipCompress(plainText) : plainText);
            var    aes        = Aes.Create();
            var    pbkdf2     = new Rfc2898DeriveBytes(password, salt, Iterations);

            aes.Key = pbkdf2.GetBytes(32); // set a 256-bit key
            aes.IV  = pbkdf2.GetBytes(16); // set a 128-bit IV
            var ms = new MemoryStream();

            using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(plainBytes, 0, plainBytes.Length);
            }
            return(Convert.ToBase64String(ms.ToArray()));
        }
Esempio n. 2
0
        private string Decrypt(string cryptoText, string password)
        {
            byte[] salt        = Encoding.Unicode.GetBytes(Salt);
            byte[] cryptoBytes = Convert.FromBase64String(Compressed ? CompressionUtility.GZipCompress(cryptoText) : cryptoText);
            var    aes         = Aes.Create();
            var    pbkdf2      = new Rfc2898DeriveBytes(password, salt, Iterations);

            aes.Key = pbkdf2.GetBytes(32);
            aes.IV  = pbkdf2.GetBytes(16);
            var ms = new MemoryStream();

            using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(cryptoBytes, 0, cryptoBytes.Length);
            }
            return(Encoding.Unicode.GetString(ms.ToArray()));
        }