Package org.pdfbox.exceptions

Examples of org.pdfbox.exceptions.CryptographyException


            this.keyLength = dictionary.getLength();
        }
       
        if(!(decryptionMaterial instanceof PublicKeyDecryptionMaterial))
        {
            throw new CryptographyException(
                "Provided decryption material is not compatible with the document");
        }
       
        PublicKeyDecryptionMaterial material = (PublicKeyDecryptionMaterial)decryptionMaterial;       
       
        try
        {
            boolean foundRecipient = false;           
       
            // the decrypted content of the enveloped data that match
            // the certificate in the decryption material provided   
            byte[] envelopedData = null;           
           
            // the bytes of each recipient in the recipients array
            byte[][] recipientFieldsBytes = new byte[dictionary.getRecipientsLength()][];
           
            int recipientFieldsLength = 0;
           
            for(int i=0; i<dictionary.getRecipientsLength(); i++)
            {
                COSString recipientFieldString = dictionary.getRecipientStringAt(i);
                byte[] recipientBytes = recipientFieldString.getBytes();
                CMSEnvelopedData data = new CMSEnvelopedData(recipientBytes);
                Iterator recipCertificatesIt = data.getRecipientInfos().getRecipients().iterator();
                while(recipCertificatesIt.hasNext())
                {
                    RecipientInformation ri =
                        (RecipientInformation)recipCertificatesIt.next();
                    // Impl: if a matching certificate was previously found it is an error,
                    // here we just don't care about it
                    if(ri.getRID().match(material.getCertificate()) && !foundRecipient)
                    {
                        foundRecipient = true;
                        envelopedData = ri.getContent(material.getPrivateKey(), "BC");           
                    }
                }
                recipientFieldsBytes[i] = recipientBytes;
                recipientFieldsLength += recipientBytes.length;
            }
            if(!foundRecipient || envelopedData == null)
            {
                throw new CryptographyException("The certificate matches no recipient entry");
            }
            if(envelopedData.length != 24)
            {
                throw new CryptographyException("The enveloped data does not contain 24 bytes");
            }
            // now envelopedData contains:
            // - the 20 bytes seed
            // - the 4 bytes of permission for the current user                         
           
            byte[] accessBytes = new byte[4];
            System.arraycopy(envelopedData, 20, accessBytes, 0, 4);
           
            currentAccessPermission = new AccessPermission(accessBytes);
            currentAccessPermission.setReadOnly();
           
             // what we will put in the SHA1 = the seed + each byte contained in the recipients array
            byte[] sha1Input = new byte[recipientFieldsLength + 20];
           
            // put the seed in the sha1 input
            System.arraycopy(envelopedData, 0, sha1Input, 0, 20);
           
            // put each bytes of the recipients array in the sha1 input
            int sha1InputOffset = 20;           
            for(int i=0; i<recipientFieldsBytes.length; i++)
            {
                System.arraycopy(
                    recipientFieldsBytes[i], 0,
                    sha1Input, sha1InputOffset, recipientFieldsBytes[i].length);
                sha1InputOffset += recipientFieldsBytes[i].length;               
            }
           
            MessageDigest md = MessageDigest.getInstance("SHA-1");           
            byte[] mdResult = md.digest(sha1Input);
           
            // we have the encryption key ...
            encryptionKey = new byte[this.keyLength/8];           
            System.arraycopy(mdResult, 0, encryptionKey, 0, this.keyLength/8);
           
            proceedDecryption();
           
           
        }
        catch(CMSException e)
        {
            throw new CryptographyException(e);
        }
        catch(KeyStoreException e)
        {
            throw new CryptographyException(e);
        }
        catch(NoSuchProviderException e)
        {
            throw new CryptographyException(e);
        }
        catch(NoSuchAlgorithmException e)
        {
            throw new CryptographyException(e);
        }
       
    }
View Full Code Here


            doc.getDocument().setEncryptionDictionary(dictionary.encryptionDictionary);           
           
        }
        catch(NoSuchAlgorithmException ex)
        {
            throw new CryptographyException(ex);
        }
        catch(NoSuchProviderException ex)
        {
            throw new CryptographyException(ex);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new CryptographyException(e);
        }
       
    }   
View Full Code Here

        document = doc;
       
        PDEncryptionDictionary dictionary = document.getEncryptionDictionary();
        if(!(decryptionMaterial instanceof StandardDecryptionMaterial))
        {
            throw new CryptographyException("Provided decryption material is not compatible with the document");
        }
       
        StandardDecryptionMaterial material = (StandardDecryptionMaterial)decryptionMaterial;
       
        String password = material.getPassword();
        if(password == null)
        {
            password = "";
        }
       
        int dicPermissions = dictionary.getPermissions();
        int dicRevision = dictionary.getRevision();
        int dicLength = dictionary.getLength()/8;

        COSString id = (COSString)document.getDocument().getDocumentID().getObject( 0 );
        byte[] u = dictionary.getUserKey();
        byte[] o = dictionary.getOwnerKey();

        boolean isUserPassword =
            isUserPassword(
                password.getBytes(),
                u,
                o,
                dicPermissions,
                id.getBytes(),
                dicRevision,
                dicLength );
        boolean isOwnerPassword =
            isOwnerPassword(
                password.getBytes(),
                u,
                o,
                dicPermissions,
                id.getBytes(),
                dicRevision,
                dicLength );

        if( isUserPassword )
        {
            encryptionKey =
                computeEncryptedKey(
                    password.getBytes(),
                    o,
                    dicPermissions,
                    id.getBytes(),
                    dicRevision,
                    dicLength );
        }
        else if( isOwnerPassword )
        {
            byte[] computedUserPassword = getUserPassword(password.getBytes(),o,dicRevision,dicLength );
            encryptionKey =
                computeEncryptedKey(
                    computedUserPassword,
                    o,
                    dicPermissions,
                    id.getBytes(),
                    dicRevision,
                    dicLength );
        }
        else
        {
            throw new CryptographyException(
                "Error: The supplied password does not match either the owner or user password in the document." );
        }
       
        this.proceedDecryption();
    }
View Full Code Here

                idArray.add( idString );
                document.getDocument().setDocumentID( idArray );
            }
            catch( NoSuchAlgorithmException e )
            {
                throw new CryptographyException( e );
            }
            catch(IOException e )
            {
                throw new CryptographyException( e );
            }
        }
       
        COSString id = (COSString)idArray.getObject( 0 );       
       
View Full Code Here

                    digest = md.digest();
                }
            }
            if( encRevision == 2 && length != 5 )
            {
                throw new CryptographyException(
                    "Error: Expected length=5 actual=" + length );
            }

            //3.3 STEP 4
            byte[] rc4Key = new byte[ (int)length ];
            System.arraycopy( digest, 0, rc4Key, 0, (int)length );

            //3.7 step 2
            if( encRevision == 2 )
            {
                rc4.setKey( rc4Key );
                rc4.write( o, result );
            }
            else if( encRevision == 3 || encRevision == 4)
            {
                /**
                byte[] iterationKey = new byte[ rc4Key.length ];
                byte[] dataToEncrypt = o;
                for( int i=19; i>=0; i-- )
                {
                    System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
                    for( int j=0; j< iterationKey.length; j++ )
                    {
                        iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
                    }
                    rc4.setKey( iterationKey );
                    rc4.write( dataToEncrypt, result );
                    dataToEncrypt = result.toByteArray();
                    result.reset();
                }
                result.write( dataToEncrypt, 0, dataToEncrypt.length );
                */
                byte[] iterationKey = new byte[ rc4Key.length ];
               
             
                byte[] otemp = new byte[ o.length ]; //sm
                System.arraycopy( o, 0, otemp, 0, o.length ); //sm
                rc4.write( o, result);//sm
               
                for( int i=19; i>=0; i-- )
                {
                    System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
                    for( int j=0; j< iterationKey.length; j++ )
                    {
                        iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
                    }
                    rc4.setKey( iterationKey );
                    result.reset()//sm
                    rc4.write( otemp, result ); //sm
                    otemp = result.toByteArray(); //sm               
                }
            }


            return result.toByteArray();

        }
        catch( NoSuchAlgorithmException e )
        {
            throw new CryptographyException( e );
        }
    }
View Full Code Here

                }

                //step 7
                if( encRevision == 2 && length != 5 )
                {
                    throw new CryptographyException(
                        "Error: length should be 5 when revision is two actual=" + length );
                }
                System.arraycopy( digest, 0, result, 0, length );
            }
            catch( NoSuchAlgorithmException e )
            {
                throw new CryptographyException( e );
            }
            return result;
        }
View Full Code Here

                    result.reset();
                    result.write( finalResult );
                }
                catch( NoSuchAlgorithmException e )
                {
                    throw new CryptographyException( e );
                }
            }
            return result.toByteArray();
        }
View Full Code Here

                        digest = md.digest();
                    }
                }
                if( encRevision == 2 && length != 5 )
                {
                    throw new CryptographyException(
                        "Error: Expected length=5 actual=" + length );
                }

                //STEP 4
                byte[] rc4Key = new byte[ length ];
                System.arraycopy( digest, 0, rc4Key, 0, length );

                //STEP 5
                byte[] paddedUser = truncateOrPad( userPassword );


                //STEP 6
                rc4.setKey( rc4Key );
                ByteArrayOutputStream crypted = new ByteArrayOutputStream();
                rc4.write( new ByteArrayInputStream( paddedUser ), crypted );


                //STEP 7
                if( encRevision == 3 || encRevision == 4 )
                {
                    byte[] iterationKey = new byte[ rc4Key.length ];
                    for( int i=1; i<20; i++ )
                    {
                        System.arraycopy( rc4Key, 0, iterationKey, 0, rc4Key.length );
                        for( int j=0; j< iterationKey.length; j++ )
                        {
                            iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
                        }
                        rc4.setKey( iterationKey );
                        ByteArrayInputStream input = new ByteArrayInputStream( crypted.toByteArray() );
                        crypted.reset();
                        rc4.write( input, crypted );
                    }
                }

                //STEP 8
                return crypted.toByteArray();
            }
            catch( NoSuchAlgorithmException e )
            {
                throw new CryptographyException( e.getMessage() );
            }
        }
View Full Code Here

            MessageDigest md = MessageDigest.getInstance( "MD5" );
            digestedKey = md.digest( newKey );
        }
        catch( NoSuchAlgorithmException e )
        {
            throw new CryptographyException( e );
        }

        //step 4
        int length = Math.min( newKey.length, 16 );
        byte[] finalKey = new byte[ length ];
View Full Code Here

            MessageDigest md = MessageDigest.getInstance( "MD5" );
            digestedKey = md.digest( newKey );
        }
        catch( NoSuchAlgorithmException e )
        {
            throw new CryptographyException( e );
        }

        //step 4
        int length = Math.min( newKey.length, 16 );
        byte[] finalKey = new byte[ length ];
View Full Code Here

TOP

Related Classes of org.pdfbox.exceptions.CryptographyException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.