Пример #1
0
        public static void RecoverPublicKey(string privateKeyPath)
        {
            bool validUserInput = FilePathValidation.RecoverPublicKey(privateKeyPath);

            if (!validUserInput)
            {
                return;
            }
            byte[] privateKey = AsymmetricKeyValidation.GetPrivateKeyFromFile(privateKeyPath);
            if (privateKey == null)
            {
                return;
            }
            privateKey = PrivateKey.Decrypt(privateKey);
            if (privateKey == null)
            {
                return;
            }
            byte[] publicKey = privateKey.Length switch
            {
                Constants.EncryptionKeyLength => AsymmetricKeys.GetCurve25519PublicKey(privateKey),
                _ => AsymmetricKeys.GetEd25519PublicKey(privateKey),
            };
            Console.WriteLine($"Public key: {Convert.ToBase64String(publicKey)}");
        }
Пример #2
0
 private static void DecryptEachFileWithPrivateKey(string[] filePaths, byte[] privateKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileDecryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         try
         {
             using var inputFile = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.RandomAccess);
             byte[] ephemeralPublicKey    = FileHeaders.ReadEphemeralPublicKey(inputFile);
             byte[] ephemeralSharedSecret = KeyExchange.GetSharedSecret(privateKey, ephemeralPublicKey);
             byte[] salt             = FileHeaders.ReadSalt(inputFile);
             byte[] keyEncryptionKey = Generate.KeyEncryptionKey(ephemeralSharedSecret, salt);
             DecryptInputFile(inputFile, ephemeralPublicKey, keyEncryptionKey);
             CryptographicOperations.ZeroMemory(keyEncryptionKey);
         }
         catch (Exception ex) when(ExceptionFilters.Cryptography(ex))
         {
             FileException(inputFilePath, ex);
         }
     }
 }
Пример #3
0
 public static void GenerateNewKeyPair(string exportDirectoryPath)
 {
     try
     {
         int  keyPairType    = GetKeyPairType();
         bool validUserInput = FilePathValidation.GenerateKeyPair(exportDirectoryPath, keyPairType);
         if (!validUserInput)
         {
             return;
         }
         string publicKey, privateKey, publicKeyPath, privateKeyPath;
         if (keyPairType == 1)
         {
             (publicKey, privateKey)         = AsymmetricKeys.GenerateEncryptionKeyPair();
             (publicKeyPath, privateKeyPath) = AsymmetricKeys.ExportEncryptionKeyPair(exportDirectoryPath, publicKey, privateKey);
         }
         else
         {
             (publicKey, privateKey)         = AsymmetricKeys.GenerateSigningKeyPair();
             (publicKeyPath, privateKeyPath) = AsymmetricKeys.ExportSigningKeyPair(exportDirectoryPath, publicKey, privateKey);
         }
         DisplayKeyPair(publicKey, publicKeyPath, privateKeyPath);
     }
     catch (Exception ex) when(ExceptionFilters.FileAccess(ex))
     {
         Logging.LogException(ex.ToString(), Logging.Severity.Error);
         DisplayMessage.FilePathException(exportDirectoryPath, ex.GetType().Name, "Unable to export key pair.");
     }
 }
Пример #4
0
 private static void DecryptEachFileWithPassword(string[] filePaths, byte[] keyEncryptionKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileDecryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         DecryptInputFile(inputFilePath, keyEncryptionKey);
     }
     Utilities.ZeroArray(keyEncryptionKey);
 }
Пример #5
0
        private static void FileEncryptionWithPassword(char[] password, string keyfilePath, string[] filePaths)
        {
            bool validUserInput = FileEncryptionValidation.FileEncryptionWithPassword(password, keyfilePath, filePaths);

            if (!validUserInput)
            {
                return;
            }
            if (!File.Exists(keyfilePath))
            {
                keyfilePath = FilePathValidation.KeyfilePath(keyfilePath);
            }
            byte[] passwordBytes = Password.Hash(password, keyfilePath);
            FileEncryption.EncryptEachFileWithPassword(filePaths, passwordBytes);
        }
Пример #6
0
 public static void EncryptEachFileWithPassword(string[] filePaths, byte[] passwordBytes)
 {
     Globals.TotalCount = filePaths.Length;
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         UsingPassword(inputFilePath, passwordBytes);
     }
     Utilities.ZeroArray(passwordBytes);
     DisplayMessage.SuccessfullyEncrypted();
 }
Пример #7
0
 public static void DecryptEachFileWithPassword(string[] filePaths, byte[] passwordBytes)
 {
     Globals.TotalCount = filePaths.Length;
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileDecryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         UsingPassword(inputFilePath, passwordBytes);
     }
     CryptographicOperations.ZeroMemory(passwordBytes);
     DisplayMessage.SuccessfullyDecrypted();
 }
Пример #8
0
 private static void EncryptEachFileWithPassword(string[] filePaths, byte[] salt, byte[] keyEncryptionKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         // Fill unused header with random public key
         byte[] ephemeralPublicKey = Generate.EphemeralPublicKeyHeader();
         string outputFilePath     = FileEncryption.GetOutputFilePath(inputFilePath);
         EncryptInputFile(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey);
     }
     CryptographicOperations.ZeroMemory(keyEncryptionKey);
 }
Пример #9
0
 private static void EncryptEachFileWithPassword(string[] filePaths, byte[] salt, byte[] keyEncryptionKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         // Fill the ephemeral public key header with random bytes (since not in use)
         byte[] randomEphemeralPublicKeyHeader = Generate.EphemeralPublicKeyHeader();
         string outputFilePath = FileEncryption.GetOutputFilePath(inputFilePath);
         EncryptInputFile(inputFilePath, outputFilePath, randomEphemeralPublicKeyHeader, salt, keyEncryptionKey);
     }
     Utilities.ZeroArray(keyEncryptionKey);
 }
Пример #10
0
 private static void DecryptEachFileWithPrivateKey(string[] filePaths, byte[] privateKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileDecryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         byte[] ephemeralPublicKey    = FileHeaders.ReadEphemeralPublicKey(inputFilePath);
         byte[] ephemeralSharedSecret = KeyExchange.GetSharedSecret(privateKey, ephemeralPublicKey);
         byte[] salt             = FileHeaders.ReadSalt(inputFilePath);
         byte[] keyEncryptionKey = Generate.KeyEncryptionKey(ephemeralSharedSecret, salt);
         DecryptInputFile(inputFilePath, keyEncryptionKey);
         Utilities.ZeroArray(keyEncryptionKey);
     }
 }
Пример #11
0
 private static void EncryptEachFileWithPrivateKey(string[] filePaths, byte[] privateKey)
 {
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         // Derive a unique KEK per file
         byte[] ephemeralSharedSecret = KeyExchange.GetPrivateKeySharedSecret(privateKey, out byte[] ephemeralPublicKey);
         byte[] salt             = Generate.Salt();
         byte[] keyEncryptionKey = Generate.KeyEncryptionKey(ephemeralSharedSecret, salt);
         string outputFilePath   = FileEncryption.GetOutputFilePath(inputFilePath);
         EncryptInputFile(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey);
         CryptographicOperations.ZeroMemory(keyEncryptionKey);
     }
 }
Пример #12
0
 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, byte[] ephemeralPublicKey) = KeyExchange.GetEphemeralSharedSecret(recipientPublicKey);
         byte[] salt             = Generate.Salt();
         byte[] keyEncryptionKey = Generate.KeyEncryptionKey(sharedSecret, ephemeralSharedSecret, salt);
         string outputFilePath   = FileEncryption.GetOutputFilePath(inputFilePath);
         EncryptInputFile(inputFilePath, outputFilePath, ephemeralPublicKey, salt, keyEncryptionKey);
         Utilities.ZeroArray(keyEncryptionKey);
     }
 }
Пример #13
0
 public static void EncryptEachFileWithPrivateKey(string[] filePaths, byte[] privateKey)
 {
     Globals.TotalCount = filePaths.Length;
     privateKey         = PrivateKey.Decrypt(privateKey);
     if (privateKey == null)
     {
         return;
     }
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         UsingPrivateKey(inputFilePath, privateKey);
     }
     Utilities.ZeroArray(privateKey);
     DisplayMessage.SuccessfullyEncrypted();
 }
Пример #14
0
        private static void VerifySignature(string publicKeyPath, string signatureFilePath, string[] filePaths)
        {
            bool validUserInput = SigningValidation.Verify(publicKeyPath, signatureFilePath, filePaths);

            if (!validUserInput)
            {
                return;
            }
            signatureFilePath = FilePathValidation.GetSignatureFilePath(signatureFilePath, filePaths);
            bool validSignatureFile = SigningValidation.SignatureFile(signatureFilePath);

            if (!validSignatureFile)
            {
                return;
            }
            byte[] publicKey = AsymmetricKeyValidation.SigningPublicKeyFile(publicKeyPath);
            if (publicKey == null)
            {
                return;
            }
            FileSigning.VerifyFile(signatureFilePath, filePaths[0], publicKey);
        }
Пример #15
0
 public static void EncryptEachFileWithPublicKey(string[] filePaths, byte[] senderPrivateKey, byte[] recipientPublicKey)
 {
     Globals.TotalCount = filePaths.Length;
     senderPrivateKey   = PrivateKey.Decrypt(senderPrivateKey);
     if (senderPrivateKey == null)
     {
         return;
     }
     byte[] sharedSecret = KeyExchange.GetSharedSecret(senderPrivateKey, recipientPublicKey);
     Utilities.ZeroArray(senderPrivateKey);
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileEncryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         UsingPublicKey(inputFilePath, sharedSecret, recipientPublicKey);
     }
     Utilities.ZeroArray(sharedSecret);
     DisplayMessage.SuccessfullyEncrypted();
 }
Пример #16
0
 public static void DecryptEachFileWithPublicKey(string[] filePaths, byte[] recipientPrivateKey, byte[] senderPublicKey)
 {
     Globals.TotalCount  = filePaths.Length;
     recipientPrivateKey = PrivateKey.Decrypt(recipientPrivateKey);
     if (recipientPrivateKey == null)
     {
         return;
     }
     byte[] sharedSecret = KeyExchange.GetSharedSecret(recipientPrivateKey, senderPublicKey);
     foreach (string inputFilePath in filePaths)
     {
         bool validFilePath = FilePathValidation.FileDecryption(inputFilePath);
         if (!validFilePath)
         {
             --Globals.TotalCount;
             continue;
         }
         UsingPublicKey(inputFilePath, sharedSecret, recipientPrivateKey);
     }
     CryptographicOperations.ZeroMemory(recipientPrivateKey);
     CryptographicOperations.ZeroMemory(sharedSecret);
     DisplayMessage.SuccessfullyDecrypted();
 }