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> /// 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); } } }