Beispiel #1
0
        ////////////////////////////////////////////////////////////////////
        // Encryption Constructor
        ////////////////////////////////////////////////////////////////////

        internal PdfEncryption
        (
            PdfDocument Document,
            String UserPassword,
            String OwnerPassword,
            Permission UserPermissions
        ) : base(Document)
        {
            // Notes:
            // The PDF File Writer library supports AES 128 encryption only.
            // The library does not strip leading or trailing white space. They are part of the password.
            // EncriptMetadata is assumed to be true (this libraray does not use metadata).
            // Embeded Files Only is assumed to be false (this library does not have embeded files).

            Dictionary.Add("/Filter", "/Standard");
            Dictionary.Add("/R", "4");
            Dictionary.Add("/V", "4");
            Dictionary.Add("/Length", "128");

            // remove all unused bits and add all bits that must be one
            Permissions = ((Int32)UserPermissions & (Int32)Permission.All) | PermissionBase;
            Dictionary.AddInteger("/P", Permissions);

            // convert user string password to byte array
            Byte[] UserBinaryPassword = ProcessPassword(UserPassword);

            // convert owner string password to byte array
            if (String.IsNullOrEmpty(OwnerPassword))
            {
                OwnerPassword = BitConverter.ToUInt64(PdfDocument.RandomByteArray(8), 0).ToString();
            }
            Byte[] OwnerBinaryPassword = ProcessPassword(OwnerPassword);

            // calculate owner key for crypto dictionary
            Byte[] OwnerKey = CreateOwnerKey(UserBinaryPassword, OwnerBinaryPassword);

            CreateMasterKey(UserBinaryPassword, OwnerKey);
            Byte[] UserKey = CreateUserKey();

            Dictionary.Add("/O", Document.ByteArrayToPdfHexString(OwnerKey));
            Dictionary.Add("/U", Document.ByteArrayToPdfHexString(UserKey));
            Dictionary.Add("/StrF", "/StdCF");
            Dictionary.Add("/StmF", "/StdCF");
            Dictionary.Add("/CF", "<</StdCF<</Length 16/AuthEvent/DocOpen/CFM/AESV2>>>>");
            return;
        }
Beispiel #2
0
        ////////////////////////////////////////////////////////////////////
        // Encryption Constructor
        ////////////////////////////////////////////////////////////////////

        internal PdfEncryption
        (
            PdfDocument Document,
            string UserPassword,
            string OwnerPassword,
            Permission UserPermissions,
            EncryptionType EncryptionType
        ) : base(Document)
        {
            // Notes:
            // The PDF File Writer library supports AES 128 encryption and standard 128 encryption.
            // The library does not strip leading or trailing white space. They are part of the password.
            // EncriptMetadata is assumed to be true (this libraray does not use metadata).
            // Embeded Files Only is assumed to be false (this library does not have embeded files).

            // remove all unused bits and add all bits that must be one
            Permissions = ((int)UserPermissions & (int)Permission.All) | PermissionBase;
            Dictionary.AddInteger("/P", Permissions);

            // convert user string password to byte array
            var UserBinaryPassword = ProcessPassword(UserPassword);

            // convert owner string password to byte array
            if (string.IsNullOrEmpty(OwnerPassword))
            {
                OwnerPassword = BitConverter.ToUInt64(PdfDocument.RandomByteArray(8), 0).ToString();
            }

            var OwnerBinaryPassword = ProcessPassword(OwnerPassword);

            // calculate owner key for crypto dictionary
            var OwnerKey = CreateOwnerKey(UserBinaryPassword, OwnerBinaryPassword);

            // create master key and user key
            CreateMasterKey(UserBinaryPassword, OwnerKey);
            var UserKey = CreateUserKey();

            // build dictionary
            Dictionary.Add("/Filter", "/Standard");
            Dictionary.Add("/Length", "128");
            Dictionary.Add("/O", Document.ByteArrayToPdfHexString(OwnerKey));
            Dictionary.Add("/U", Document.ByteArrayToPdfHexString(UserKey));

            // encryption type
            this.EncryptionType = EncryptionType;
            if (EncryptionType == EncryptionType.Aes128)
            {
                Dictionary.Add("/R", "4");
                Dictionary.Add("/V", "4");
                Dictionary.Add("/StrF", "/StdCF");
                Dictionary.Add("/StmF", "/StdCF");
                Dictionary.Add("/CF", "<</StdCF<</Length 16/AuthEvent/DocOpen/CFM/AESV2>>>>");
            }
            else
            {
                Dictionary.Add("/R", "3");
                Dictionary.Add("/V", "2");
            }

            // add encryption to trailer dictionary
            Document.TrailerDict.AddIndirectReference("/Encrypt", this);
        }