/// <summary>Creates the encryption.</summary>
        /// <param name="userPassword">
        /// the user password. Can be null or of zero length, which is equal to
        /// omitting the user password
        /// </param>
        /// <param name="ownerPassword">
        /// the owner password. If it's null or empty, iText will generate
        /// a random string to be used as the owner password
        /// </param>
        /// <param name="permissions">
        /// the user permissions
        /// The open permissions for the document can be
        /// <see cref="EncryptionConstants.ALLOW_PRINTING"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_MODIFY_CONTENTS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_COPY"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_MODIFY_ANNOTATIONS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_FILL_IN"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_SCREENREADERS"/>
        /// ,
        /// <see cref="EncryptionConstants.ALLOW_ASSEMBLY"/>
        /// and
        /// <see cref="EncryptionConstants.ALLOW_DEGRADED_PRINTING"/>.
        /// The permissions can be combined by ORing them
        /// </param>
        /// <param name="encryptionType">
        /// the type of encryption. It can be one of
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_40"/>
        /// ,
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_128"/>
        /// ,
        /// <see cref="EncryptionConstants.ENCRYPTION_AES_128"/>
        /// or
        /// <see cref="EncryptionConstants.ENCRYPTION_AES_256"/>.
        /// Optionally
        /// <see cref="EncryptionConstants.DO_NOT_ENCRYPT_METADATA"/>
        /// can be
        /// ORed to output the metadata in cleartext.
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// can be ORed as well.
        /// Please be aware that the passed encryption types may override permissions:
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_40"/>
        /// implicitly sets
        /// <see cref="EncryptionConstants.DO_NOT_ENCRYPT_METADATA"/>
        /// and
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// as false;
        /// <see cref="EncryptionConstants.STANDARD_ENCRYPTION_128"/>
        /// implicitly sets
        /// <see cref="EncryptionConstants.EMBEDDED_FILES_ONLY"/>
        /// as false;
        /// </param>
        /// <param name="documentId">document id which will be used for encryption</param>
        /// <param name="version">
        /// the
        /// <see cref="PdfVersion"/>
        /// of the target document for encryption
        /// </param>
        public PdfEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType, byte[]
                             documentId, PdfVersion version)
            : base(new PdfDictionary())
        {
            this.documentId = documentId;
            if (version != null && version.CompareTo(PdfVersion.PDF_2_0) >= 0)
            {
                permissions = FixAccessibilityPermissionPdf20(permissions);
            }
            int revision = SetCryptoMode(encryptionType);

            switch (revision)
            {
            case STANDARD_ENCRYPTION_40: {
                StandardHandlerUsingStandard40 handlerStd40 = new StandardHandlerUsingStandard40(this.GetPdfObject(), userPassword
                                                                                                 , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerStd40.GetPermissions();
                securityHandler  = handlerStd40;
                break;
            }

            case STANDARD_ENCRYPTION_128: {
                StandardHandlerUsingStandard128 handlerStd128 = new StandardHandlerUsingStandard128(this.GetPdfObject(), userPassword
                                                                                                    , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerStd128.GetPermissions();
                securityHandler  = handlerStd128;
                break;
            }

            case AES_128: {
                StandardHandlerUsingAes128 handlerAes128 = new StandardHandlerUsingAes128(this.GetPdfObject(), userPassword
                                                                                          , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerAes128.GetPermissions();
                securityHandler  = handlerAes128;
                break;
            }

            case AES_256: {
                StandardHandlerUsingAes256 handlerAes256 = new StandardHandlerUsingAes256(this.GetPdfObject(), userPassword
                                                                                          , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, version);
                this.permissions = handlerAes256.GetPermissions();
                securityHandler  = handlerAes256;
                break;
            }
            }
        }
        public PdfEncryption(PdfDictionary pdfDict, byte[] password, byte[] documentId)
            : base(pdfDict)
        {
            SetForbidRelease();
            this.documentId = documentId;
            int revision = ReadAndSetCryptoModeForStdHandler(pdfDict);

            switch (revision)
            {
            case STANDARD_ENCRYPTION_40: {
                StandardHandlerUsingStandard40 handlerStd40 = new StandardHandlerUsingStandard40(this.GetPdfObject(), password
                                                                                                 , documentId, encryptMetadata);
                permissions     = handlerStd40.GetPermissions();
                securityHandler = handlerStd40;
                break;
            }

            case STANDARD_ENCRYPTION_128: {
                StandardHandlerUsingStandard128 handlerStd128 = new StandardHandlerUsingStandard128(this.GetPdfObject(), password
                                                                                                    , documentId, encryptMetadata);
                permissions     = handlerStd128.GetPermissions();
                securityHandler = handlerStd128;
                break;
            }

            case AES_128: {
                StandardHandlerUsingAes128 handlerAes128 = new StandardHandlerUsingAes128(this.GetPdfObject(), password, documentId
                                                                                          , encryptMetadata);
                permissions     = handlerAes128.GetPermissions();
                securityHandler = handlerAes128;
                break;
            }

            case AES_256: {
                StandardHandlerUsingAes256 aes256Handler = new StandardHandlerUsingAes256(this.GetPdfObject(), password);
                permissions     = aes256Handler.GetPermissions();
                encryptMetadata = aes256Handler.IsEncryptMetadata();
                securityHandler = aes256Handler;
                break;
            }
            }
        }
Beispiel #3
0
        /// <summary>Creates the encryption.</summary>
        /// <remarks>
        /// Creates the encryption. The userPassword and the
        /// ownerPassword can be null or have zero length. In this case the ownerPassword
        /// is replaced by a random string. The open permissions for the document can be
        /// AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
        /// AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
        /// The permissions can be combined by ORing them.
        /// </remarks>
        /// <param name="userPassword">the user password. Can be null or empty</param>
        /// <param name="ownerPassword">the owner password. Can be null or empty</param>
        /// <param name="permissions">the user permissions</param>
        /// <param name="encryptionType">
        /// the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
        /// Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
        /// </param>
        /// <exception cref="iText.Kernel.PdfException">if the document is already open</exception>
        public PdfEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType, byte[]
                             documentId)
            : base(new PdfDictionary())
        {
            this.documentId = documentId;
            int revision = SetCryptoMode(encryptionType);

            switch (revision)
            {
            case STANDARD_ENCRYPTION_40: {
                StandardHandlerUsingStandard40 handlerStd40 = new StandardHandlerUsingStandard40(this.GetPdfObject(), userPassword
                                                                                                 , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerStd40.GetPermissions();
                securityHandler  = handlerStd40;
                break;
            }

            case STANDARD_ENCRYPTION_128: {
                StandardHandlerUsingStandard128 handlerStd128 = new StandardHandlerUsingStandard128(this.GetPdfObject(), userPassword
                                                                                                    , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerStd128.GetPermissions();
                securityHandler  = handlerStd128;
                break;
            }

            case AES_128: {
                StandardHandlerUsingAes128 handlerAes128 = new StandardHandlerUsingAes128(this.GetPdfObject(), userPassword
                                                                                          , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly, documentId);
                this.permissions = handlerAes128.GetPermissions();
                securityHandler  = handlerAes128;
                break;
            }

            case AES_256: {
                StandardHandlerUsingAes256 handlerAes256 = new StandardHandlerUsingAes256(this.GetPdfObject(), userPassword
                                                                                          , ownerPassword, permissions, encryptMetadata, embeddedFilesOnly);
                this.permissions = handlerAes256.GetPermissions();
                securityHandler  = handlerAes256;
                break;
            }
            }
        }