public bool EncryptAndSaveFile(string filePath, MemoryStream ms, string passwordString, CryptoProgress progress) { FileStream fs = null; try { CryptoProgressHandler progressHandler = null; if (string.IsNullOrEmpty(passwordString)) { throw new Exception("Password can not be null or empty"); } if (File.Exists(filePath)) { fs = new FileStream(filePath, FileMode.Truncate); } else { fs = new FileStream(filePath, FileMode.CreateNew); } // fs = new FileStream(filePath, FileMode.Truncate); //File.Create(filePath); if (progress != null) { progressHandler = new CryptoProgressHandler { EncodedBytes = 0, TotalBytes = ms.Length, Text = "Starting ecryption" }; progress.Report(progressHandler); } using (Aes aesAlg = Aes.Create()) { var rfc2898DeriveBytes = new Rfc2898DeriveBytes(passwordString, SALT, 1000); Debug.Assert(aesAlg != null, nameof(aesAlg) + " != null"); aesAlg.BlockSize = 128; aesAlg.KeySize = 256; aesAlg.Padding = PaddingMode.PKCS7; aesAlg.Mode = CipherMode.CBC; aesAlg.Key = rfc2898DeriveBytes.GetBytes(32); aesAlg.IV = rfc2898DeriveBytes.GetBytes(16); // Create a encrypt transform ICryptoTransform encrypt = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption. int bufferSize = (int)Math.Min(MaxBufferSize, ms.Length); var buffer = new byte[bufferSize]; ms.Position = 0; using (var csEncrypt = new CryptoStream(fs, encrypt, CryptoStreamMode.Write)) { int bytesRead; while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) > 0) { csEncrypt.Write(buffer, 0, bytesRead); if (progressHandler == null) { continue; } progressHandler.EncodedBytes += bytesRead; progress.Report(progressHandler); } csEncrypt.FlushFinalBlock(); fs.Flush(); } } } catch (Exception ex) { Log.Error(ex, "Error in EncryptionManager.EncryptAndSaveFile"); return(false); } finally { fs?.Close(); progress?.Report(new CryptoProgressHandler { EncodedBytes = ms.Length, TotalBytes = ms.Length, Text = "Encryption completed" }); } return(true); }
public MemoryStream DecryptFileToMemoryStream(string filePath, string passwordString, CryptoProgress progress) { var ms = new MemoryStream(); FileStream fs = null; try { CryptoProgressHandler progressHandler = null; if (string.IsNullOrEmpty(passwordString)) { throw new Exception("Password can not be null or empty"); } fs = File.OpenRead(filePath); fs.Position = 0; if (progress != null) { progressHandler = new CryptoProgressHandler { EncodedBytes = 0, TotalBytes = fs.Length, Text = "Starting decryption" }; progress.Report(progressHandler); } // Create an AesCryptoServiceProvider object using (Aes aesAlg = Aes.Create()) { var rfc2898DeriveBytes = new Rfc2898DeriveBytes(passwordString, SALT, 1000); aesAlg.BlockSize = 128; aesAlg.KeySize = 256; aesAlg.Padding = PaddingMode.PKCS7; aesAlg.Mode = CipherMode.CBC; aesAlg.Key = rfc2898DeriveBytes.GetBytes(32); aesAlg.IV = rfc2898DeriveBytes.GetBytes(16); // Create a decrytor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption. int bufferSize = Math.Min(MaxBufferSize, (int)fs.Length); var plainTextBytes = new byte[bufferSize]; using (var csDecrypt = new CryptoStream(fs, decryptor, CryptoStreamMode.Read)) { int decryptedByteCount; while ((decryptedByteCount = csDecrypt.Read(plainTextBytes, 0, plainTextBytes.Length)) > 0) { ms.Write(plainTextBytes, 0, decryptedByteCount); if (progressHandler == null) { continue; } progressHandler.EncodedBytes += decryptedByteCount; progress.Report(progressHandler); } } } } catch (Exception ex) { Log.Error(ex, "Error in EncryptionManager.EncryptAndSaveFile"); return(null); } finally { fs?.Close(); progress?.Report(new CryptoProgressHandler { EncodedBytes = ms.Length, TotalBytes = ms.Length, Text = "Decryption completed" }); } return(ms); }