/// <summary> /// Encrypts the specified file. All file types are supported. /// </summary> /// <param name="inFile">The path to the file to be encrypted. Path must exist.</param> /// <param name="outFile">he path in which to write the encrypted file.</param> /// <param name="wipeTimesToWrite">Performs n-pass forensic wipe of the disk sectors where the input file was stored.</param> public void EncryptFile(string inFile, string outFile, int wipeTimesToWrite = 0) { using (X509CryptoAgent Agent = new X509CryptoAgent(this)) { Agent.EncryptFile(inFile, outFile); } if (!File.Exists(outFile)) { throw new X509CryptoException($"Unable to encrypt the file \"{inFile}\". The ciphertext file \"{outFile}\" could not be created."); } if (wipeTimesToWrite > 0) { X509Utils.WipeFile(inFile, wipeTimesToWrite); } }
/// <summary> /// Encrypts the specified file /// </summary> /// <param name="thumbprint">The thumbprint of the certificate to use for encryption</param> /// <param name="plaintextFilePath">The fully-qualified path of the plaintext file (can be text or binary)</param> /// <param name="Context">(Optional) The certificate store where the encryption certificate resides (Default: <see cref="X509Context"/>.<see cref="X509Context.UserReadOnly"/>)</param> /// <param name="ciphertextFilePath">(Optional) The fully-qualified path in which to write the encrypted file (If not specified, the plaintext file path is appended with a ".ctx" extension)</param> /// <param name="verbose">(Optional) True enables verbose logging</param> /// <returns></returns> /// <example> /// <code> /// string thumbprint = @"ccdc673c40ebb2a433300c0c8a2ba6f443da5688"; /// <see cref="X509Context"/> certStore = <see cref="X509Context"/>.<see cref="X509Context.UserReadOnly"/> /// string plaintextFilePath = @"C:\Data\accounts.csv"; /// string ciphertextFilePath = /// bool success = <see cref="X509Utils"/>.EncryptFile(thumbprint, plaintextFilePath, certStore); /// </code> /// </example> public static bool EncryptFile(string thumbprint, string plaintextFilePath, X509Context Context = null, string ciphertextFilePath = "", bool verbose = false) { CheckForFile(plaintextFilePath); if (Context == null) { Context = X509Context.UserReadOnly; } if (string.IsNullOrEmpty(ciphertextFilePath)) { ciphertextFilePath = plaintextFilePath + CRYPTO_ENCRYPTED_FILE_EXT; } File.Delete(ciphertextFilePath); using (X509CryptoAgent cryptoAgent = new X509CryptoAgent(FormatThumbprint(thumbprint), Context)) { cryptoAgent.EncryptFile(plaintextFilePath, ciphertextFilePath); } return(File.Exists(ciphertextFilePath)); }