public void Encrypt(Stream dataStream, Stream encryptedStream, string password = null) { byte [] key = PasswordDeriver.DeriveKey(Password ?? password); ICryptoTransform cryptoTransform = SymmetricAlgorithm.CreateEncryptor(key, InitialVector); using (var cryptoStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)) { dataStream.CopyTo(cryptoStream); } }
public byte[] Decrypt(Stream encryptedDataStream, string password = null) { byte [] key = PasswordDeriver.DeriveKey(Password ?? password); ICryptoTransform cryptoTransform = SymmetricAlgorithm.CreateDecryptor(key, InitialVector); using (var cryptoStream = new CryptoStream(encryptedDataStream, cryptoTransform, CryptoStreamMode.Read)) { using (var streamReader = new StreamReader(cryptoStream, Encoding.Default)) { string decryptedText = streamReader.ReadToEnd(); return(Encoding.Default.GetBytes(decryptedText)); } } }