Exemple #1
0
        public void Security(string PDF,
                             string userPassword,
                             PasswordValidity userPasswordValidity,
                             string ownerPassword,
                             PasswordValidity ownerPasswordValidity)
        {
            string errorMsg = string.Empty;

            string filePath = Path.Combine(TestHelper.DataFolder, PDF);

            Assert.IsTrue(File.Exists(filePath));
            PdfFile pdfFile = new PdfFile(filePath);

            pdfFile.Open(ref errorMsg);
            Assert.IsTrue(string.IsNullOrEmpty(errorMsg));
            IDecryptor pdfDecryptor = DecryptorFactory.Get(pdfFile.EncryptionRecordInfo);

            PasswordValidity passwordValidity =
                pdfDecryptor.ValidatePassword(userPassword, ValidationMode.ValidateUserPassword);

            Assert.AreEqual(passwordValidity, userPasswordValidity);

            passwordValidity = pdfDecryptor.ValidatePassword(ownerPassword, ValidationMode.ValidateOwnerPassword);
            Assert.AreEqual(passwordValidity, ownerPasswordValidity);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            DecryptorFactory decryptorFactory     = new DecryptorFactory();
            IDecryptor       applicationDecryptor = decryptorFactory.CreateDecryptor();

            AuthorizerFactory authorizerFactory     = new AuthorizerFactory();
            IAuthorizer       applicationAuthorizer = authorizerFactory.CreateAuthorizer();

            Context applicationContext = new Context(applicationDecryptor, applicationAuthorizer);
            UI      userInterface      = new UI();

            bool readAnotherFile = false;

            do
            {
                applicationContext = userInterface.AddUserInfoToContext(applicationContext);

                // handles opening file and displaying content to the console
                FileReader ApplicationFileReader = new FileReader(applicationContext);
                ApplicationFileReader.DisplayContent();

                // ask user if application should read another file
                readAnotherFile = userInterface.AskReadAnotherFile();
            } while (readAnotherFile);
        }
        private void DecryptWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            TimeSpan  ts;
            const int timeSlice = 50;
            const int nrOfSpeedMeasurements = 50;
            ulong     passwordCount = 0, prevPasswordCount = 0;

            object[]          args             = (object[])e.Argument;
            IDecryptor        decryptor        = DecryptorFactory.Get((EncryptionRecord)args[0]);
            IPasswordIterator passwordIterator = (IPasswordIterator)args[1];
            ValidationMode    validationMode   = (ValidationMode)args[2];
            IMovingAverage    avg              = new SimpleMovingAverage(nrOfSpeedMeasurements);
            PasswordValidity  passwordValidity = PasswordValidity.Invalid;
            Stopwatch         stopWatch        = new Stopwatch();

            stopWatch.Start();
            string currentPassword = passwordIterator.GetNextPassword();

            while (!string.IsNullOrEmpty(currentPassword) &&
                   validationMode != ValidationMode.None &&
                   !decryptBackgroundWorker.CancellationPending)
            {
                passwordValidity = decryptor.ValidatePassword(currentPassword, validationMode);

                passwordCount++;
                ts = stopWatch.Elapsed;
                if (ts.Milliseconds > timeSlice || passwordValidity != PasswordValidity.Invalid)
                {
                    avg.AddSample((60000 * (passwordCount - prevPasswordCount)) / ((ulong)ts.Milliseconds + 1)); //Avoid div by zero
                    decryptBackgroundWorker.ReportProgress(0, new object[]
                                                           { passwordValidity, currentPassword, (ulong)avg.Average, passwordCount });
                    prevPasswordCount = passwordCount;
                    stopWatch.Restart();

                    if ((passwordValidity & PasswordValidity.OwnerPasswordIsValid) == PasswordValidity.OwnerPasswordIsValid)
                    {
                        validationMode &= ~ValidationMode.ValidateOwnerPassword;
                    }

                    if ((passwordValidity & PasswordValidity.UserPasswordIsValid) == PasswordValidity.UserPasswordIsValid)
                    {
                        validationMode &= ~ValidationMode.ValidateUserPassword;
                    }
                }
                currentPassword = passwordIterator.GetNextPassword();
            }

            if (decryptBackgroundWorker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                e.Result = passwordCount;
            }
        }
 public PdfLoginViewModel(Window pdfLoginWindow, EncryptionRecord encryptionRecord)
 {
     PdfDecryptor = DecryptorFactory.Get(encryptionRecord);
     pdfLoginView = pdfLoginWindow;
     InitializeCommands();
 }
Exemple #5
0
        public int Open(ref string errorMsg)
        {
            int result     = Constants.Failure;
            int cryptoMode = 0;

            try
            {
                PdfReader pdfReader = new PdfReader(Info.FullName,
                                                    out encryptionRecordInfo.pdfVersion,
                                                    out encryptionRecordInfo.documentID,
                                                    out encryptionRecordInfo.uValue,
                                                    out encryptionRecordInfo.oValue,
                                                    out encryptionRecordInfo.pValue,
                                                    out encryptionRecordInfo.rValue,
                                                    out cryptoMode,
                                                    out encryptionRecordInfo.isEncrypted,
                                                    out encryptionRecordInfo.keyLength);

                if (encryptionRecordInfo.isEncrypted &&
                    (encryptionRecordInfo.uValue == null || encryptionRecordInfo.oValue == null))
                {
                    errorMsg = "PDF encryption type not supported.";
                }
                else if (encryptionRecordInfo.isEncrypted)
                {
                    //When using AES encryption, the option is available to refrain from encrypting the metadata.
                    encryptionRecordInfo.metadataIsEncrypted =
                        (cryptoMode & PdfWriter.DO_NOT_ENCRYPT_METADATA) != PdfWriter.DO_NOT_ENCRYPT_METADATA;

                    if ((cryptoMode & PdfWriter.ENCRYPTION_MASK) == PdfWriter.STANDARD_ENCRYPTION_40)
                    {
                        encryptionRecordInfo.encryptionType      = PdfEncryptionType.StandardEncryption40Bit;
                        encryptionRecordInfo.metadataIsEncrypted = true;
                    }
                    else if ((cryptoMode & PdfWriter.ENCRYPTION_MASK) == PdfWriter.STANDARD_ENCRYPTION_128)
                    {
                        encryptionRecordInfo.encryptionType      = PdfEncryptionType.StandardEncryption128Bit;
                        encryptionRecordInfo.metadataIsEncrypted = true;
                    }
                    else if ((cryptoMode & PdfWriter.ENCRYPTION_MASK) == PdfWriter.ENCRYPTION_AES_128)
                    {
                        encryptionRecordInfo.encryptionType = PdfEncryptionType.AesEncryption128Bit;
                    }
                    else if ((cryptoMode & PdfWriter.ENCRYPTION_MASK) == PdfWriter.ENCRYPTION_AES_256)
                    {
                        encryptionRecordInfo.encryptionType  = PdfEncryptionType.AesEncryption256Bit;
                        encryptionRecordInfo.MaxPasswordSize = Constants.MaxPasswordSizeV2;
                        encryptionRecordInfo.PasswordCharset = CharsetEncoding.Unicode;
                    }
                    else if ((cryptoMode & PdfWriter.ENCRYPTION_MASK) == PdfWriter.ENCRYPTION_AES_256_ISO)
                    {
                        encryptionRecordInfo.encryptionType  = PdfEncryptionType.AesIsoEncryption256Bit;
                        encryptionRecordInfo.MaxPasswordSize = Constants.MaxPasswordSizeV2;
                        encryptionRecordInfo.PasswordCharset = CharsetEncoding.Unicode;
                    }

                    IDecryptor       pdfDecryptor     = DecryptorFactory.Get(EncryptionRecordInfo);
                    PasswordValidity passwordValidity = pdfDecryptor.ValidatePassword(string.Empty,
                                                                                      ValidationMode.ValidateUserPassword | ValidationMode.ValidateOwnerPassword);
                    UserPasswordIsSet =
                        (passwordValidity & PasswordValidity.UserPasswordIsValid) != PasswordValidity.UserPasswordIsValid;
                    OwnerPasswordIsSet =
                        (passwordValidity & PasswordValidity.OwnerPasswordIsValid) != PasswordValidity.OwnerPasswordIsValid;
                }

                result = Constants.Success;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
            }
            return(result);
        }