Пример #1
0
        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); }));
                }
            }
        }
Пример #2
0
        /// <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.");
            }
        }