예제 #1
0
        /// <summary>Returns the value associated to this key as a Boolean.</summary>
        /// <remarks>Returns the value associated to this key as a Boolean. If the value isn't a PdfBoolean, null is returned.
        ///     </remarks>
        /// <param name="key">the key of which the associated value needs to be returned</param>
        /// <returns>Boolean associated with this key</returns>
        public virtual bool?GetAsBool(PdfName key)
        {
            PdfBoolean b            = GetAsBoolean(key);
            bool?      booleanValue = null;

            if (b != null)
            {
                booleanValue = b.GetValue();
            }
            return(booleanValue);
        }
예제 #2
0
        private int ReadAndSetCryptoModeForPubSecHandler(PdfDictionary encDict)
        {
            int       cryptoMode;
            int       length = 0;
            PdfNumber vValue = encDict.GetAsNumber(PdfName.V);

            if (vValue == null)
            {
                throw new PdfException(PdfException.IllegalVValue);
            }
            int v = vValue.IntValue();

            switch (v)
            {
            case 1: {
                cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_40;
                length     = 40;
                break;
            }

            case 2: {
                PdfNumber lengthValue = encDict.GetAsNumber(PdfName.Length);
                if (lengthValue == null)
                {
                    throw new PdfException(PdfException.IllegalLengthValue);
                }
                length = lengthValue.IntValue();
                if (length > 128 || length < 40 || length % 8 != 0)
                {
                    throw new PdfException(PdfException.IllegalLengthValue);
                }
                cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_128;
                break;
            }

            case 4:
            case 5: {
                PdfDictionary dic = encDict.GetAsDictionary(PdfName.CF);
                if (dic == null)
                {
                    throw new PdfException(PdfException.CfNotFoundEncryption);
                }
                dic = (PdfDictionary)dic.Get(PdfName.DefaultCryptFilter);
                if (dic == null)
                {
                    throw new PdfException(PdfException.DefaultcryptfilterNotFoundEncryption);
                }
                if (PdfName.V2.Equals(dic.Get(PdfName.CFM)))
                {
                    cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_128;
                    length     = 128;
                }
                else
                {
                    if (PdfName.AESV2.Equals(dic.Get(PdfName.CFM)))
                    {
                        cryptoMode = EncryptionConstants.ENCRYPTION_AES_128;
                        length     = 128;
                    }
                    else
                    {
                        if (PdfName.AESV3.Equals(dic.Get(PdfName.CFM)))
                        {
                            cryptoMode = EncryptionConstants.ENCRYPTION_AES_256;
                            length     = 256;
                        }
                        else
                        {
                            throw new PdfException(PdfException.NoCompatibleEncryptionFound);
                        }
                    }
                }
                PdfBoolean em = dic.GetAsBoolean(PdfName.EncryptMetadata);
                if (em != null && !em.GetValue())
                {
                    cryptoMode |= EncryptionConstants.DO_NOT_ENCRYPT_METADATA;
                }
                break;
            }

            default: {
                throw new PdfException(PdfException.UnknownEncryptionTypeVEq1, vValue);
            }
            }
            return(SetCryptoMode(cryptoMode, length));
        }
예제 #3
0
        private int ReadAndSetCryptoModeForStdHandler(PdfDictionary encDict)
        {
            int       cryptoMode;
            int       length = 0;
            PdfNumber rValue = encDict.GetAsNumber(PdfName.R);

            if (rValue == null)
            {
                throw new PdfException(PdfException.IllegalRValue);
            }
            int revision = rValue.IntValue();

            switch (revision)
            {
            case 2: {
                cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_40;
                break;
            }

            case 3: {
                PdfNumber lengthValue = encDict.GetAsNumber(PdfName.Length);
                if (lengthValue == null)
                {
                    throw new PdfException(PdfException.IllegalLengthValue);
                }
                length = lengthValue.IntValue();
                if (length > 128 || length < 40 || length % 8 != 0)
                {
                    throw new PdfException(PdfException.IllegalLengthValue);
                }
                cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_128;
                break;
            }

            case 4: {
                PdfDictionary dic = (PdfDictionary)encDict.Get(PdfName.CF);
                if (dic == null)
                {
                    throw new PdfException(PdfException.CfNotFoundEncryption);
                }
                dic = (PdfDictionary)dic.Get(PdfName.StdCF);
                if (dic == null)
                {
                    throw new PdfException(PdfException.StdcfNotFoundEncryption);
                }
                if (PdfName.V2.Equals(dic.Get(PdfName.CFM)))
                {
                    cryptoMode = EncryptionConstants.STANDARD_ENCRYPTION_128;
                }
                else
                {
                    if (PdfName.AESV2.Equals(dic.Get(PdfName.CFM)))
                    {
                        cryptoMode = EncryptionConstants.ENCRYPTION_AES_128;
                    }
                    else
                    {
                        throw new PdfException(PdfException.NoCompatibleEncryptionFound);
                    }
                }
                PdfBoolean em = encDict.GetAsBoolean(PdfName.EncryptMetadata);
                if (em != null && !em.GetValue())
                {
                    cryptoMode |= EncryptionConstants.DO_NOT_ENCRYPT_METADATA;
                }
                break;
            }

            case 5:
            case 6: {
                cryptoMode = EncryptionConstants.ENCRYPTION_AES_256;
                PdfBoolean em5 = encDict.GetAsBoolean(PdfName.EncryptMetadata);
                if (em5 != null && !em5.GetValue())
                {
                    cryptoMode |= EncryptionConstants.DO_NOT_ENCRYPT_METADATA;
                }
                break;
            }

            default: {
                throw new PdfException(PdfException.UnknownEncryptionTypeREq1).SetMessageParams(rValue);
            }
            }
            revision = SetCryptoMode(cryptoMode, length);
            return(revision);
        }