private static void UsingPublicKey(string inputFilePath, byte[] sharedSecret, byte[] recipientPublicKey) { try { bool fileIsDirectory = FileHandling.IsDirectory(inputFilePath); if (fileIsDirectory) { DirectoryEncryption.UsingPublicKey(inputFilePath, sharedSecret, recipientPublicKey); return; } // Derive a unique KEK per file (byte[] ephemeralSharedSecret, byte[] ephemeralPublicKey) = KeyExchange.GetEphemeralSharedSecret(recipientPublicKey); byte[] salt = Generate.Salt(); byte[] keyEncryptionKey = Generate.KeyEncryptionKey(sharedSecret, ephemeralSharedSecret, salt); string outputFilePath = GetOutputFilePath(inputFilePath); EncryptFile.Initialize(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey); Utilities.ZeroArray(keyEncryptionKey); EncryptionSuccessful(inputFilePath, outputFilePath); } catch (Exception ex) when(ExceptionFilters.FileAccess(ex)) { Logging.LogException(ex.ToString(), Logging.Severity.Error); DisplayMessage.FilePathException(inputFilePath, ex.GetType().Name, "Unable to encrypt the file."); } }
private static void EncryptEachFileWithPublicKey(string[] filePaths, byte[] sharedSecret, byte[] recipientPublicKey) { foreach (string inputFilePath in filePaths) { bool validFilePath = FilePathValidation.FileEncryption(inputFilePath); if (!validFilePath) { --Globals.TotalCount; continue; } // Derive a unique KEK per file byte[] ephemeralSharedSecret = KeyExchange.GetEphemeralSharedSecret(recipientPublicKey, out byte[] ephemeralPublicKey); byte[] salt = Generate.Salt(); byte[] keyEncryptionKey = Generate.KeyEncryptionKey(sharedSecret, ephemeralSharedSecret, salt); string outputFilePath = FileEncryption.GetOutputFilePath(inputFilePath); EncryptInputFile(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey); CryptographicOperations.ZeroMemory(keyEncryptionKey); } }