⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 standardsecurityhandler.java

📁 非常有用的操作pdf文件的java源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                }
            }
            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 );
        }
    }
    
    /**
     * Compute the encryption key.
     * 
     * @param password The password to compute the encrypted key.
     * @param o The o entry of the encryption dictionary.
     * @param permissions The permissions for the document.
     * @param id The document id.
     * @param encRevision The revision of the encryption algorithm.
     * @param length The length of the encryption key.
     * 
     * @return The encrypted key bytes.
     * 
     * @throws CryptographyException If there is an error with encryption.
     */
    public final byte[] computeEncryptedKey(
            byte[] password,
            byte[] o,
            int permissions,
            byte[] id,
            int encRevision,
            int length )
            throws CryptographyException
        {
            byte[] result = new byte[ length ];
            try
            {
                //PDFReference 1.4 pg 78
                //step1
                byte[] padded = truncateOrPad( password );

                //step 2
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update( padded );

                //step 3
                md.update( o );

                //step 4
                byte zero = (byte)(permissions >>> 0);
                byte one = (byte)(permissions >>> 8);
                byte two = (byte)(permissions >>> 16);
                byte three = (byte)(permissions >>> 24);

                md.update( zero );
                md.update( one );
                md.update( two );
                md.update( three );

                //step 5
                md.update( id );
                byte[] digest = md.digest();

                //step 6
                if( encRevision == 3 || encRevision == 4)
                {
                    for( int i=0; i<50; i++ )
                    {
                        md.reset();
                        md.update( digest, 0, length );
                        digest = md.digest();
                    }
                }

                //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;
        }
    
    /**
     * This will compute the user password hash.
     *
     * @param password The plain text password.
     * @param o The owner password hash.
     * @param permissions The document permissions.
     * @param id The document id.
     * @param encRevision The revision of the encryption.
     * @param length The length of the encryption key.
     *
     * @return The user password.
     *
     * @throws CryptographyException If there is an error computing the user password.
     * @throws IOException If there is an IO error.
     */
    
    public final byte[] computeUserPassword(
            byte[] password,
            byte[] o,
            int permissions,
            byte[] id,
            int encRevision,
            int length )
            throws CryptographyException, IOException
        {
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            //STEP 1
            byte[] encryptionKey = computeEncryptedKey( password, o, permissions, id, encRevision, length );

            if( encRevision == 2 )
            {
                //STEP 2
                rc4.setKey( encryptionKey );
                rc4.write( ENCRYPT_PADDING, result );
            }
            else if( encRevision == 3 || encRevision == 4 )
            {
                try
                {
                    //STEP 2
                    MessageDigest md = MessageDigest.getInstance("MD5");
                    //md.update( truncateOrPad( password ) );
                    md.update( ENCRYPT_PADDING );

                    //STEP 3
                    md.update( id );
                    result.write( md.digest() );

                    //STEP 4 and 5
                    byte[] iterationKey = new byte[ encryptionKey.length ];
                    for( int i=0; i<20; i++ )
                    {
                        System.arraycopy( encryptionKey, 0, iterationKey, 0, iterationKey.length );
                        for( int j=0; j< iterationKey.length; j++ )
                        {
                            iterationKey[j] = (byte)(iterationKey[j] ^ i);
                        }
                        rc4.setKey( iterationKey );
                        ByteArrayInputStream input = new ByteArrayInputStream( result.toByteArray() );
                        result.reset();
                        rc4.write( input, result );
                    }

                    //step 6
                    byte[] finalResult = new byte[32];
                    System.arraycopy( result.toByteArray(), 0, finalResult, 0, 16 );
                    System.arraycopy( ENCRYPT_PADDING, 0, finalResult, 16, 16 );
                    result.reset();
                    result.write( finalResult );
                }
                catch( NoSuchAlgorithmException e )
                {
                    throw new CryptographyException( e );
                }
            }
            return result.toByteArray();
        }
    
    /**
     * Compute the owner entry in the encryption dictionary.
     * 
     * @param ownerPassword The plaintext owner password.
     * @param userPassword The plaintext user password.
     * @param encRevision The revision number of the encryption algorithm.
     * @param length The length of the encryption key.
     * 
     * @return The o entry of the encryption dictionary.
     * 
     * @throws CryptographyException If there is an error with encryption.
     * @throws IOException If there is an error accessing data.
     */
    public final byte[] computeOwnerPassword(
            byte[] ownerPassword,
            byte[] userPassword,
            int encRevision,
            int length )
            throws CryptographyException, IOException
        {
            try
            {
                //STEP 1
                byte[] ownerPadded = truncateOrPad( ownerPassword );

                //STEP 2
                MessageDigest md = MessageDigest.getInstance( "MD5" );
                md.update( ownerPadded );
                byte[] digest = md.digest();

                //STEP 3
                if( encRevision == 3 || encRevision == 4)
                {
                    for( int i=0; i<50; i++ )
                    {
                        md.reset();
                        md.update( digest, 0, length );
                        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() );
            }
        }
    

    /**
     * This will take the password and truncate or pad it as necessary.
     *
     * @param password The password to pad or truncate.
     *
     * @return The padded or truncated password.
     */
    private final byte[] truncateOrPad( byte[] password )
    {
        byte[] padded = new byte[ ENCRYPT_PADDING.length ];
        int bytesBeforePad = Math.min( password.length, padded.length );
        System.arraycopy( password, 0, padded, 0, bytesBeforePad );
        System.arraycopy( ENCRYPT_PADDING, 0, padded, bytesBeforePad, ENCRYPT_PADDING.length-bytesBeforePad );
        return padded;
    }
    
    /**
     * Check if a plaintext password is the user password.
     * 
     * @param password The plaintext password.
     * @param u The u entry of the encryption dictionary.
     * @param o The o entry of the encryption dictionary.
     * @param permissions The permissions set in the the PDF.
     * @param id The document id used for encryption.
     * @param encRevision The revision of the encryption algorithm.
     * @param length The length of the encryption key.
     * 
     * @return true If the plaintext password is the user password.
     * 
     * @throws CryptographyException If there is an error during encryption.
     * @throws IOException If there is an error accessing data.
     */
    public final boolean isUserPassword(
            byte[] password,
            byte[] u,
            byte[] o,
            int permissions,
            byte[] id,
            int encRevision,
            int length)
            throws CryptographyException, IOException
        {
            boolean matches = false;
            //STEP 1
            byte[] computedValue = computeUserPassword( password, o, permissions, id, encRevision, length );
            if( encRevision == 2 )
            {
                //STEP 2
                matches = arraysEqual( u, computedValue );
            }
            else if( encRevision == 3 || encRevision == 4 )
            {
                //STEP 2
                matches = arraysEqual( u, computedValue, 16 );
            }
            return matches;
        }
    
    private static final boolean arraysEqual( byte[] first, byte[] second, int count )
    {
        boolean equal = first.length >= count && second.length >= count;
        for( int i=0; i<count && equal; i++ )
        {
            equal = first[i] == second[i];
        }
        return equal;
    }

    /**
     * This will compare two byte[] for equality.
     *
     * @param first The first byte array.
     * @param second The second byte array.
     *
     * @return true If the arrays contain the exact same data.
     */
    private static final boolean arraysEqual( byte[] first, byte[] second )
    {
        boolean equal = first.length == second.length;
        for( int i=0; i<first.length && equal; i++ )
        {
            equal = first[i] == second[i];
        }
        return equal;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -