public static byte[] Encrypt(this byte[] Data, DeriveBytes Key, SymmetricAlgorithm AlgorithmUsing = null, string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(InitialVector), "InitialVector"); if (Data==null) return null; AlgorithmUsing = AlgorithmUsing.Check(()=>new RijndaelManaged()); using (DeriveBytes DerivedPassword = Key) { using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing) { SymmetricKey.Mode = CipherMode.CBC; byte[] CipherTextBytes = null; using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray())) { using (MemoryStream MemStream = new MemoryStream()) { using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)) { CryptoStream.Write(Data, 0, Data.Length); CryptoStream.FlushFinalBlock(); CipherTextBytes = MemStream.ToArray(); } } } SymmetricKey.Clear(); return CipherTextBytes; } } }