Esempio n. 1
0
        public static byte[] Encrypt(string text, string password, byte[] salt = null)
        {
            if (string.IsNullOrEmpty(text)) throw new ArgumentNullException("text");
            if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");

            if (salt == null) salt = DefaultSalt;
            var aes = new AesManaged();

            byte[] encryptedData;

            try
            {
                aes.SetKey(password, salt);
                var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
                using (var ms = new MemoryStream())
                {
                    ms.Write(BitConverter.GetBytes(aes.IV.Length), 0, sizeof(int));
                    ms.Write(aes.IV, 0, aes.IV.Length);
                    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    using (var sw = new StreamWriter(cs))
                        sw.Write(text);
                    encryptedData = ms.ToArray();
                }
            }
            finally
            {
                aes.Clear();
            }

            return encryptedData;
        }
Esempio n. 2
0
        public static string Decrypt(byte[] encryptedData, string password, byte[] salt = null)
        {
            if (encryptedData == null) throw new ArgumentNullException("encryptedData");
            if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");

            if (salt == null) salt = DefaultSalt;
            var aes = new AesManaged();

            string text;

            try
            {
                aes.SetKey(password, salt);
                using (var ms = new MemoryStream(encryptedData))
                {
                    aes.IV = ms.GetIV();
                    var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
                    using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    using (var sr = new StreamReader(cs))
                        text = sr.ReadToEnd();
                }
            }
            finally
            {
                aes.Clear();
            }

            return text;
        }