Beispiel #1
0
        /// <summary>
        /// Prepares the security handler for encrypting the document.
        /// Entry point for encryption of a document that is about to be saved.
        /// </summary>
        public void PrepareEncryption()
        {
#if !SILVERLIGHT
            Debug.Assert(this._document._securitySettings.DocumentSecurityLevel != PdfDocumentSecurityLevel.None);
            int  permissions      = (int)this.Permission;
            bool strongEncryption = this._document._securitySettings.DocumentSecurityLevel >= PdfDocumentSecurityLevel.Encrypted128Bit;

            stringEncryptor = EncryptorFactory.InitEncryption(_document, this);
            streamEncryptor = stringEncryptor;

            // using the AES encryption implies a Pdf Version 1.7 ?
            //if (_document._securitySettings.DocumentSecurityLevel >= PdfDocumentSecurityLevel.EncryptedAES256 && _document.Version < 17)
            //    _document.Version = 17;

            // Correct permission bits
            permissions |= (int)(strongEncryption ? (uint)0xfffff0c0 : (uint)0xffffffc0);
            permissions &= unchecked ((int)0xfffffffc);

            PdfInteger pValue = new PdfInteger(permissions);
            Elements[Keys.P] = pValue;
#endif
        }
Beispiel #2
0
        /// <summary>
        /// Checks the password.
        /// Entry point for decrypting a loaded document
        /// </summary>
        /// <param name="inputPassword">Password or null if no password is provided.</param>
        public PasswordValidity ValidatePassword(string inputPassword)
        {
            if (inputPassword == null)
            {
                inputPassword = "";
            }

            EncryptorFactory.InitDecryption(_document, this, out stringEncryptor, out streamEncryptor);
            if (stringEncryptor == null && streamEncryptor == null)
            {
                throw new PdfSharpException("No suitable decryptor available. Send this document to support.");
            }

            if (streamEncryptor == null && stringEncryptor != null)
            {
                streamEncryptor = stringEncryptor;
            }
            else if (stringEncryptor == null)
            {
                stringEncryptor = streamEncryptor;
            }

            stringEncryptor.InitEncryptionKey(inputPassword);
            streamEncryptor.InitEncryptionKey(inputPassword);

            stringEncryptor.ValidatePassword(inputPassword);

            if (stringEncryptor.PasswordValid && stringEncryptor.HaveOwnerPermission)
            {
                return(PasswordValidity.OwnerPassword);
            }
            if (stringEncryptor.PasswordValid)
            {
                return(PasswordValidity.UserPassword);
            }
            return(PasswordValidity.Invalid);
        }