示例#1
0
 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;
     }
 }
示例#2
0
 private static byte[] Decrypt(byte[] passwordBytes, byte[] privateKey)
 {
     byte[] keyAlgorithm        = GetKeyAlgorithm(privateKey);
     byte[] keyVersion          = GetKeyVersion(privateKey);
     byte[] salt                = GetSalt(privateKey);
     byte[] nonce               = GetNonce(privateKey);
     byte[] additionalData      = Utilities.ConcatArrays(keyAlgorithm, keyVersion);
     byte[] encryptedPrivateKey = GetEncryptedPrivateKey(privateKey);
     byte[] key = Argon2.DeriveKey(passwordBytes, salt);
     Utilities.ZeroArray(passwordBytes);
     byte[] decryptedPrivateKey = SecretAeadXChaCha20Poly1305.Decrypt(encryptedPrivateKey, nonce, key, additionalData);
     Utilities.ZeroArray(key);
     ChunkHandling.ValidateKeyCommitmentBlock(decryptedPrivateKey);
     return(ChunkHandling.RemoveKeyCommitmentBlock(decryptedPrivateKey));
 }
示例#3
0
        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);
        }