public static void Initialize(string inputFilePath, string outputFilePath, byte[] keyEncryptionKey) { byte[] dataEncryptionKey = new byte[Constants.EncryptionKeyLength]; try { byte[] encryptedHeader = FileHeaders.ReadEncryptedHeader(inputFilePath); byte[] nonce = FileHeaders.ReadNonce(inputFilePath); byte[] header = DecryptFileHeader(inputFilePath, encryptedHeader, nonce, keyEncryptionKey); if (header == null) { throw new ArgumentException("Incorrect password/keyfile or this file has been tampered with."); } ChunkHandling.ValidateKeyCommitmentBlock(header); int lastChunkLength = FileHeaders.GetLastChunkLength(header); int fileNameLength = FileHeaders.GetFileNameLength(header); dataEncryptionKey = FileHeaders.GetDataEncryptionKey(header); Utilities.ZeroArray(header); using (var inputFile = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.SequentialScan)) using (var outputFile = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.SequentialScan)) { nonce = Sodium.Utilities.Increment(nonce); byte[] additionalData = ChunkHandling.GetPreviousPoly1305Tag(encryptedHeader); Decrypt(inputFile, outputFile, nonce, dataEncryptionKey, additionalData, lastChunkLength); } Finalize(inputFilePath, outputFilePath, fileNameLength); } catch (Exception ex) when(ExceptionFilters.Cryptography(ex)) { Utilities.ZeroArray(dataEncryptionKey); throw; } }
private static void Encrypt(FileStream inputFile, FileStream outputFile, byte[] nonce, byte[] dataEncryptionKey, byte[] additionalData) { const int offset = 0; byte[] plaintext = new byte[Constants.FileChunkSize]; while (inputFile.Read(plaintext, offset, plaintext.Length) > 0) { byte[] plaintextChunk = ChunkHandling.PrependKeyCommitmentBlock(plaintext); byte[] ciphertextChunk = SecretAeadXChaCha20Poly1305.Encrypt(plaintextChunk, nonce, dataEncryptionKey, additionalData); nonce = Sodium.Utilities.Increment(nonce); additionalData = ChunkHandling.GetPreviousPoly1305Tag(ciphertextChunk); outputFile.Write(ciphertextChunk, offset, ciphertextChunk.Length); } Utilities.ZeroArray(dataEncryptionKey); }
private static void Decrypt(FileStream inputFile, FileStream outputFile, byte[] nonce, byte[] dataEncryptionKey, byte[] additionalData, int lastChunkLength) { int headersLength = FileHeaders.GetHeadersLength(); inputFile.Seek(headersLength, SeekOrigin.Begin); const int offset = 0; byte[] ciphertextChunk = new byte[Constants.TotalChunkLength]; while (inputFile.Read(ciphertextChunk, offset, ciphertextChunk.Length) > 0) { byte[] plaintextChunk = SecretAeadXChaCha20Poly1305.Decrypt(ciphertextChunk, nonce, dataEncryptionKey, additionalData); ChunkHandling.ValidateKeyCommitmentBlock(plaintextChunk); nonce = Sodium.Utilities.Increment(nonce); additionalData = ChunkHandling.GetPreviousPoly1305Tag(ciphertextChunk); plaintextChunk = ChunkHandling.RemoveKeyCommitmentBlock(plaintextChunk); outputFile.Write(plaintextChunk, offset, plaintextChunk.Length); } outputFile.SetLength((outputFile.Length - Constants.FileChunkSize) + lastChunkLength); Utilities.ZeroArray(dataEncryptionKey); }
public static void Initialize(string inputFilePath, string outputFilePath, byte[] ephemeralPublicKey, byte[] salt, byte[] keyEncryptionKey) { byte[] dataEncryptionKey = Generate.DataEncryptionKey(); try { using (var inputFile = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.SequentialScan)) using (var outputFile = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.SequentialScan)) { byte[] nonce = Generate.Nonce(); byte[] encryptedHeader = EncryptFileHeader(inputFilePath, dataEncryptionKey, nonce, keyEncryptionKey); FileHeaders.WriteHeaders(outputFile, ephemeralPublicKey, salt, nonce, encryptedHeader); nonce = Sodium.Utilities.Increment(nonce); byte[] additionalData = ChunkHandling.GetPreviousPoly1305Tag(encryptedHeader); Encrypt(inputFile, outputFile, nonce, dataEncryptionKey, additionalData); } Finalize(inputFilePath, outputFilePath); } catch (Exception ex) when(ExceptionFilters.Cryptography(ex)) { FileHandling.DeleteFile(outputFilePath); Utilities.ZeroArray(dataEncryptionKey); throw; } }