public static void Encrypt(IRuntimeFileInfo sourceFile, Stream destinationStream, AesKey key, AxCryptOptions options, ProgressContext progress) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (destinationStream == null) { throw new ArgumentNullException("destinationStream"); } if (key == null) { throw new ArgumentNullException("key"); } if (progress == null) { throw new ArgumentNullException("progress"); } using (Stream sourceStream = sourceFile.OpenRead()) { using (AxCryptDocument document = new AxCryptDocument()) { DocumentHeaders headers = new DocumentHeaders(key); headers.FileName = sourceFile.Name; headers.CreationTimeUtc = sourceFile.CreationTimeUtc; headers.LastAccessTimeUtc = sourceFile.LastAccessTimeUtc; headers.LastWriteTimeUtc = sourceFile.LastWriteTimeUtc; document.DocumentHeaders = headers; document.EncryptTo(headers, sourceStream, destinationStream, options, progress); } } }
/// <summary> /// Decrypt a source file to a destination file, given a passphrase /// </summary> /// <param name="sourceFile">The source file</param> /// <param name="destinationFile">The destination file</param> /// <param name="passphrase">The passphrase</param> /// <returns>true if the passphrase was correct</returns> public static string Decrypt(IRuntimeFileInfo sourceFile, string destinationDirectory, AesKey key, AxCryptOptions options, ProgressContext progress) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (destinationDirectory == null) { throw new ArgumentNullException("destinationDirectory"); } if (key == null) { throw new ArgumentNullException("key"); } if (progress == null) { throw new ArgumentNullException("progress"); } string destinationFileName = null; using (AxCryptDocument document = Document(sourceFile, key, new ProgressContext())) { if (!document.PassphraseIsValid) { return(destinationFileName); } destinationFileName = document.DocumentHeaders.FileName; IRuntimeFileInfo destinationFullPath = OS.Current.FileInfo(Path.Combine(destinationDirectory, destinationFileName)); Decrypt(document, destinationFullPath, options, progress); } return(destinationFileName); }
/// <summary> /// Decrypt a source file to a destination file, given a passphrase /// </summary> /// <param name="sourceFile">The source file</param> /// <param name="destinationFile">The destination file</param> /// <param name="passphrase">The passphrase</param> /// <returns>true if the passphrase was correct</returns> public static bool Decrypt(IRuntimeFileInfo sourceFile, IRuntimeFileInfo destinationFile, AesKey key, AxCryptOptions options, ProgressContext progress) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (destinationFile == null) { throw new ArgumentNullException("destinationFile"); } if (key == null) { throw new ArgumentNullException("key"); } if (progress == null) { throw new ArgumentNullException("progress"); } using (AxCryptDocument document = Document(sourceFile, key, new ProgressContext())) { if (!document.PassphraseIsValid) { return(false); } Decrypt(document, destinationFile, options, progress); } return(true); }
/// <summary> /// Decrypt from loaded AxCryptDocument to a destination file /// </summary> /// <param name="document">The loaded AxCryptDocument</param> /// <param name="destinationFile">The destination file</param> public static void Decrypt(AxCryptDocument document, IRuntimeFileInfo destinationFile, AxCryptOptions options, ProgressContext progress) { if (document == null) { throw new ArgumentNullException("document"); } if (destinationFile == null) { throw new ArgumentNullException("destinationFile"); } if (progress == null) { throw new ArgumentNullException("progress"); } try { if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Decrypting to '{0}'.".InvariantFormat(destinationFile.Name)); } using (Stream destinationStream = destinationFile.OpenWrite()) { document.DecryptTo(destinationStream, progress); } if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Decrypted to '{0}'.".InvariantFormat(destinationFile.Name)); } } catch (OperationCanceledException) { if (destinationFile.Exists) { AxCryptFile.Wipe(destinationFile, progress); } throw; } if (options.HasMask(AxCryptOptions.SetFileTimes)) { DocumentHeaders headers = document.DocumentHeaders; destinationFile.SetFileTimes(headers.CreationTimeUtc, headers.LastAccessTimeUtc, headers.LastWriteTimeUtc); } }
/// <summary> /// Encrypt a file /// </summary> /// <param name="file">The file to encrypt</param> /// <param name="destination">The destination file</param> /// <remarks>It is the callers responsibility to ensure that the source file exists, that the destination file /// does not exist and can be created etc.</remarks> public static void Encrypt(IRuntimeFileInfo sourceFile, IRuntimeFileInfo destinationFile, Passphrase passphrase, AxCryptOptions options, ProgressContext progress) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (destinationFile == null) { throw new ArgumentNullException("destinationFile"); } if (passphrase == null) { throw new ArgumentNullException("passphrase"); } if (progress == null) { throw new ArgumentNullException("progress"); } using (Stream sourceStream = sourceFile.OpenRead()) { using (Stream destinationStream = destinationFile.OpenWrite()) { using (AxCryptDocument document = new AxCryptDocument()) { DocumentHeaders headers = new DocumentHeaders(passphrase.DerivedPassphrase); headers.FileName = sourceFile.Name; headers.CreationTimeUtc = sourceFile.CreationTimeUtc; headers.LastAccessTimeUtc = sourceFile.LastAccessTimeUtc; headers.LastWriteTimeUtc = sourceFile.LastWriteTimeUtc; document.DocumentHeaders = headers; document.EncryptTo(headers, sourceStream, destinationStream, options, progress); } } if (options.HasMask(AxCryptOptions.SetFileTimes)) { destinationFile.SetFileTimes(sourceFile.CreationTimeUtc, sourceFile.LastAccessTimeUtc, sourceFile.LastWriteTimeUtc); } } }
/// <summary> /// Load an AxCryptDocument from a source file with a passphrase /// </summary> /// <param name="sourceFile">The source file</param> /// <param name="passphrase">The passphrase</param> /// <returns>An instance of AxCryptDocument. Use IsPassphraseValid property to determine validity.</returns> public static AxCryptDocument Document(IRuntimeFileInfo sourceFile, AesKey key, ProgressContext progress) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (key == null) { throw new ArgumentNullException("key"); } if (progress == null) { throw new ArgumentNullException("progress"); } AxCryptDocument document = new AxCryptDocument(); Stream stream = new ProgressStream(sourceFile.OpenRead(), progress); progress.AddTotal(stream.Length); document.Load(stream, key); return(document); }
/// <summary> /// Load an AxCryptDocument from a source file with a passphrase /// </summary> /// <param name="sourceFile">The source file</param> /// <param name="passphrase">The passphrase</param> /// <returns>An instance of AxCryptDocument. Use IsPassphraseValid property to determine validity.</returns> public static AxCryptDocument Document(IRuntimeFileInfo sourceFile, AesKey key, ProgressContext progress) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (key == null) { throw new ArgumentNullException("key"); } if (progress == null) { throw new ArgumentNullException("progress"); } AxCryptDocument document = new AxCryptDocument(); Stream stream = new ProgressStream(sourceFile.OpenRead(), progress); progress.AddTotal(stream.Length); document.Load(stream, key); return document; }