private static ActiveFile TryDecrypt(IRuntimeFileInfo sourceFileInfo, IRuntimeFileInfo destinationFolderInfo, IEnumerable <AesKey> keys, ProgressContext progress) { ActiveFile destinationActiveFile = null; foreach (AesKey key in keys) { if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Decrypting '{0}'".InvariantFormat(sourceFileInfo.FullName)); } using (FileLock sourceLock = FileLock.Lock(sourceFileInfo)) { using (AxCryptDocument document = AxCryptFile.Document(sourceFileInfo, key, new ProgressContext())) { if (!document.PassphraseIsValid) { continue; } destinationActiveFile = DecryptActiveFileDocument(sourceFileInfo, destinationFolderInfo, document, progress); break; } } } return(destinationActiveFile); }
public static void TestSmallEncryptDecrypt() { IRuntimeFileInfo sourceFileInfo = OS.Current.FileInfo(_testTextPath); IRuntimeFileInfo destinationFileInfo = sourceFileInfo.CreateEncryptedName(); Assert.That(destinationFileInfo.Name, Is.EqualTo("test-txt.axx"), "Wrong encrypted file name based on the plain text file name."); AxCryptFile.Encrypt(sourceFileInfo, destinationFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); using (AxCryptDocument document = AxCryptFile.Document(destinationFileInfo, new Passphrase("axcrypt").DerivedPassphrase, new ProgressContext())) { Assert.That(document.PassphraseIsValid, Is.True, "The passphrase should be ok."); Assert.That(document.DocumentHeaders.FileName, Is.EqualTo("test.txt"), "Unexpected file name in headers."); Assert.That(document.DocumentHeaders.CreationTimeUtc, Is.EqualTo(FakeRuntimeFileInfo.TestDate1Utc)); Assert.That(document.DocumentHeaders.LastAccessTimeUtc, Is.EqualTo(FakeRuntimeFileInfo.TestDate2Utc)); Assert.That(document.DocumentHeaders.LastWriteTimeUtc, Is.EqualTo(FakeRuntimeFileInfo.TestDate3Utc)); IRuntimeFileInfo decryptedFileInfo = OS.Current.FileInfo(Path.Combine(_rootPath, "decrypted test.txt")); AxCryptFile.Decrypt(document, decryptedFileInfo, AxCryptOptions.SetFileTimes, new ProgressContext()); using (Stream decryptedStream = decryptedFileInfo.OpenRead()) { string decrypted = new StreamReader(decryptedStream, Encoding.UTF8).ReadToEnd(); Assert.That(decrypted, Is.EqualTo("This is a short file")); } Assert.That(decryptedFileInfo.CreationTimeUtc, Is.EqualTo(document.DocumentHeaders.CreationTimeUtc), "We're expecting file times to be set as the original from the headers."); Assert.That(decryptedFileInfo.LastAccessTimeUtc, Is.EqualTo(document.DocumentHeaders.LastAccessTimeUtc), "We're expecting file times to be set as the original from the headers."); Assert.That(decryptedFileInfo.LastWriteTimeUtc, Is.EqualTo(document.DocumentHeaders.LastWriteTimeUtc), "We're expecting file times to be set as the original from the headers."); } }
private bool OpenAxCryptDocument(string fullName, FileOperationEventArgs e) { e.AxCryptDocument = null; try { IRuntimeFileInfo source = OS.Current.FileInfo(fullName); e.OpenFileFullName = source.FullName; foreach (AesKey key in _fileSystemState.KnownKeys.Keys) { e.AxCryptDocument = AxCryptFile.Document(source, key, new ProgressContext()); if (e.AxCryptDocument.PassphraseIsValid) { break; } e.AxCryptDocument.Dispose(); e.AxCryptDocument = null; } Passphrase passphrase; while (e.AxCryptDocument == null) { OnQueryDecryptionPassphrase(e); if (e.Cancel) { e.Status = FileOperationStatus.Canceled; return(false); } passphrase = new Passphrase(e.Passphrase); e.AxCryptDocument = AxCryptFile.Document(source, passphrase.DerivedPassphrase, new ProgressContext()); if (!e.AxCryptDocument.PassphraseIsValid) { e.AxCryptDocument.Dispose(); e.AxCryptDocument = null; continue; } e.Key = passphrase.DerivedPassphrase; OnKnownKeyAdded(e); } } catch (IOException ioex) { if (e.AxCryptDocument != null) { e.AxCryptDocument.Dispose(); e.AxCryptDocument = null; } FileOperationStatus status = ioex is FileNotFoundException ? FileOperationStatus.FileDoesNotExist : FileOperationStatus.Exception; e.Status = status; return(false); } return(true); }
public static void TestInvalidArguments() { IRuntimeFileInfo sourceFileInfo = OS.Current.FileInfo(_testTextPath); IRuntimeFileInfo destinationFileInfo = sourceFileInfo.CreateEncryptedName(); AxCryptDocument document = new AxCryptDocument(); IRuntimeFileInfo decryptedFileInfo = OS.Current.FileInfo(Path.Combine(_rootPath, "decrypted test.txt")); AxCryptDocument nullDocument = null; IRuntimeFileInfo nullFileInfo = null; AesKey nullKey = null; ProgressContext nullProgress = null; Passphrase nullPassphrase = null; Stream nullStream = null; string nullString = null; Action <Stream> nullStreamAction = null; Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(nullFileInfo, destinationFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, nullFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, destinationFileInfo, nullPassphrase, AxCryptOptions.EncryptWithCompression, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, destinationFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(nullFileInfo, new MemoryStream(), new AesKey(), AxCryptOptions.None, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, nullStream, new AesKey(), AxCryptOptions.None, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, new MemoryStream(), nullKey, AxCryptOptions.None, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, new MemoryStream(), new AesKey(), AxCryptOptions.None, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullDocument, decryptedFileInfo, AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(document, nullFileInfo, AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(document, decryptedFileInfo, AxCryptOptions.SetFileTimes, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullFileInfo, decryptedFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, nullFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, decryptedFileInfo, nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, decryptedFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullFileInfo, Path.Combine(_rootPath, "Directory"), new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, nullString, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), new AesKey(), AxCryptOptions.SetFileTimes, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(nullFileInfo, new AesKey(), new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(sourceFileInfo, nullKey, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(sourceFileInfo, new AesKey(), nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.WriteToFileWithBackup(null, (Stream stream) => { }, new ProgressContext()); }); IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_testTextPath); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.WriteToFileWithBackup(fileInfo, nullStreamAction, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.MakeAxCryptFileName(nullFileInfo); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Wipe(nullFileInfo, new ProgressContext()); }); }
public static void TestEncryptToStream() { IRuntimeFileInfo sourceFileInfo = OS.Current.FileInfo(_testTextPath); IRuntimeFileInfo destinationFileInfo = sourceFileInfo.CreateEncryptedName(); Assert.That(destinationFileInfo.Name, Is.EqualTo("test-txt.axx"), "Wrong encrypted file name based on the plain text file name."); using (Stream destinationStream = destinationFileInfo.OpenWrite()) { AxCryptFile.Encrypt(sourceFileInfo, destinationStream, new Passphrase("axcrypt").DerivedPassphrase, AxCryptOptions.EncryptWithCompression, new ProgressContext()); } using (AxCryptDocument document = AxCryptFile.Document(destinationFileInfo, new Passphrase("axcrypt").DerivedPassphrase, new ProgressContext())) { Assert.That(document.PassphraseIsValid, Is.True, "The passphrase should be ok."); } }
private static ActiveFile CheckKeysForAlreadyDecryptedFile(ActiveFile destinationActiveFile, IEnumerable <AesKey> keys, ProgressContext progress) { foreach (AesKey key in keys) { using (AxCryptDocument document = AxCryptFile.Document(destinationActiveFile.EncryptedFileInfo, key, progress)) { if (document.PassphraseIsValid) { if (OS.Log.IsWarningEnabled) { OS.Log.LogWarning("File was already decrypted and the key was known for '{0}' to '{1}'".InvariantFormat(destinationActiveFile.EncryptedFileInfo.FullName, destinationActiveFile.DecryptedFileInfo.FullName)); } return(new ActiveFile(destinationActiveFile, key)); } } } return(null); }