public void TestDecryptWithCancel() { IDataStore sourceFileInfo = New <IDataStore>(_helloWorldAxxPath); Passphrase passphrase = new Passphrase("a"); IProgressContext progress = new CancelProgressContext(new ProgressContext(new TimeSpan(0, 0, 0, 0, 100))); progress.Progressing += (object sender, ProgressEventArgs e) => { progress.Cancel = true; }; Headers headers = new Headers(); AxCryptReaderBase reader = headers.CreateReader(new LookAheadStream(new ProgressStream(sourceFileInfo.OpenRead(), progress))); using (IAxCryptDocument document = AxCryptReaderBase.Document(reader)) { bool keyIsOk = document.Load(passphrase, new V1Aes128CryptoFactory().CryptoId, headers); Assert.That(keyIsOk, Is.True, "The passphrase provided is correct!"); IDataStore destinationInfo = New <IDataStore>(_rootPath.PathCombine("Destination", "Decrypted.txt")); FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; environment.CurrentTiming.CurrentTiming = new TimeSpan(0, 0, 0, 0, 100); using (FileLock destinationFileLock = New <FileLocker>().Acquire(destinationInfo)) { Assert.Throws <OperationCanceledException>((TestDelegate)(() => { New <AxCryptFile>().Decrypt(document, destinationFileLock, AxCryptOptions.None, progress); })); } } }
public bool Load(Passphrase key, Guid cryptoId, Stream inputStream) { Headers headers = new Headers(); AxCryptReader reader = headers.CreateReader(new LookAheadStream(inputStream)); return(Load(key, cryptoId, reader, headers)); }
public virtual Headers Headers(Stream inputStream) { Headers headers = new Headers(); headers.CreateReader(new LookAheadStream(inputStream)); return(headers); }
/// <summary> /// Instantiate an instance of IAxCryptDocument appropriate for the file provided, i.e. V1 or V2. /// </summary> /// <param name="decryptionParameters">The possible decryption parameters to try.</param> /// <param name="inputStream">The input stream.</param> /// <returns></returns> public virtual IAxCryptDocument CreateDocument(IEnumerable <DecryptionParameter> decryptionParameters, Stream inputStream) { if (decryptionParameters == null) { throw new ArgumentNullException(nameof(decryptionParameters)); } Headers headers = new Headers(); AxCryptReaderBase reader = headers.CreateReader(new LookAheadStream(inputStream)); IAxCryptDocument document = AxCryptReaderBase.Document(reader); foreach (DecryptionParameter decryptionParameter in decryptionParameters) { if (decryptionParameter.Passphrase != null) { document.Load(decryptionParameter.Passphrase, decryptionParameter.CryptoId, headers); if (document.PassphraseIsValid) { document.DecryptionParameter = decryptionParameter; return(document); } } if (decryptionParameter.PrivateKey != null) { document.Load(decryptionParameter.PrivateKey, decryptionParameter.CryptoId, headers); if (document.PassphraseIsValid) { document.DecryptionParameter = decryptionParameter; return(document); } } } return(document); }
public async Task TestEncryptFileWhenDestinationExists() { IDataStore sourceInfo = New <IDataStore>(_davidCopperfieldTxtPath); IDataStore expectedDestinationInfo = New <IDataStore>(AxCryptFile.MakeAxCryptFileName(sourceInfo)); using (Stream stream = expectedDestinationInfo.OpenWrite()) { } FileOperationsController controller = new FileOperationsController(); string destinationPath = String.Empty; LogOnIdentity logOnIdentity = null; controller.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) => { e.LogOnIdentity = new LogOnIdentity("allan"); }; controller.QuerySaveFileAs += (object sender, FileOperationEventArgs e) => { e.SaveFileFullName = Path.Combine(Path.GetDirectoryName(e.SaveFileFullName), "alternative-name.axx"); }; Guid cryptoId = Guid.Empty; controller.Completed += (object sender, FileOperationEventArgs e) => { destinationPath = e.SaveFileFullName; logOnIdentity = e.LogOnIdentity; cryptoId = e.CryptoId; }; FileOperationContext status = await controller.EncryptFileAsync(New <IDataStore>(_davidCopperfieldTxtPath)); Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should indicate success."); Assert.That(Path.GetFileName(destinationPath), Is.EqualTo("alternative-name.axx"), "The alternative name should be used, since the default existed."); IDataStore destinationInfo = New <IDataStore>(destinationPath); Assert.That(destinationInfo.IsAvailable, "After encryption the destination file should be created."); EncryptionParameters encryptionParameters = new EncryptionParameters(cryptoId, logOnIdentity); await encryptionParameters.AddAsync(logOnIdentity.PublicKeys); Headers headers = new Headers(); AxCryptReaderBase reader = headers.CreateReader(new LookAheadStream(destinationInfo.OpenRead())); using (IAxCryptDocument document = AxCryptReaderBase.Document(reader)) { document.Load(logOnIdentity.Passphrase, cryptoId, headers); Assert.That(document.PassphraseIsValid, "The encrypted document should be valid and encrypted with the passphrase given."); } }
public void TestReaderNotPositionedAtData() { MemoryStream encryptedFile = new MemoryStream(Resources.david_copperfield_key__aa_ae_oe__ulu_txt); Headers headers = new Headers(); AxCryptReader reader = headers.CreateReader(new LookAheadStream(encryptedFile)); using (V1AxCryptDocument document = new V1AxCryptDocument(reader)) { Passphrase key = new Passphrase("Å ä Ö"); bool keyIsOk = document.Load(key, new V1Aes128CryptoFactory().CryptoId, headers); Assert.That(keyIsOk, Is.True); reader.SetStartOfData(); Assert.Throws <InvalidOperationException>(() => document.DecryptTo(Stream.Null)); } }
public void TestDecryptToWithReaderWronglyPositioned() { using (MemoryStream inputStream = new MemoryStream()) { byte[] text = Resolve.RandomGenerator.Generate(1000); inputStream.Write(text, 0, text.Length); inputStream.Position = 0; byte[] buffer = new byte[2500]; using (IAxCryptDocument document = new V2AxCryptDocument(new EncryptionParameters(new V2Aes256CryptoFactory().CryptoId, new Passphrase("passphrase")), 113)) { document.EncryptTo(inputStream, new MemoryStream(buffer), AxCryptOptions.EncryptWithCompression); Headers headers = new Headers(); AxCryptReader reader = headers.CreateReader(new LookAheadStream(new MemoryStream(buffer))); using (V2AxCryptDocument decryptedDocument = new V2AxCryptDocument(reader)) { Assert.That(decryptedDocument.Load(new Passphrase("passphrase"), new V2Aes256CryptoFactory().CryptoId, headers), Is.True); reader.SetStartOfData(); Assert.Throws <InvalidOperationException>(() => decryptedDocument.DecryptTo(Stream.Null)); } } } }
public void TestHmacThrowsWhenTooLittleData() { using (MemoryStream plaintext = new MemoryStream(Resources.uncompressable_zip)) { MemoryStream encryptedFile = new MemoryStream(); using (V1AxCryptDocument encryptingDocument = new V1AxCryptDocument(new Passphrase("a"), 10)) { encryptingDocument.EncryptTo(plaintext, encryptedFile, AxCryptOptions.EncryptWithoutCompression); } encryptedFile.Position = 0; Headers headers = new Headers(); AxCryptReader reader = headers.CreateReader(new LookAheadStream(encryptedFile)); using (V1AxCryptDocument document = new V1AxCryptDocument(reader)) { Passphrase key = new Passphrase("a"); bool keyIsOk = document.Load(key, new V1Aes128CryptoFactory().CryptoId, headers); Assert.That(keyIsOk, Is.True); reader.InputStream.Read(new byte[16], 0, 16); Assert.Throws <InvalidOperationException>(() => document.DecryptTo(Stream.Null)); } } }