You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2015/05/03 17:44:49 UTC

svn commit: r1677425 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption: SecurityHandler.java StandardSecurityHandler.java

Author: tilman
Date: Sun May  3 15:44:49 2015
New Revision: 1677425

URL: http://svn.apache.org/r1677425
Log:
PDFBOX-2576: refactored to make field rc4 private

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java?rev=1677425&r1=1677424&r2=1677425&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/SecurityHandler.java Sun May  3 15:44:49 2015
@@ -76,7 +76,7 @@ public abstract class SecurityHandler
     protected byte[] encryptionKey;
 
     /** The RC4 implementation used for cryptographic functions. */
-    protected RC4Cipher rc4 = new RC4Cipher();
+    private final RC4Cipher rc4 = new RC4Cipher();
 
     /** indicates if the Metadata have to be decrypted of not. */ 
     private boolean decryptMetadata; 
@@ -156,8 +156,7 @@ public abstract class SecurityHandler
             }
             else
             {
-                rc4.setKey(finalKey);
-                rc4.write(data, output);
+                encryptDataRC4(finalKey, data, output);
             }
         }
         output.flush();
@@ -199,6 +198,38 @@ public abstract class SecurityHandler
     }
     
     /**
+     * Encrypt or decrypt data with RC4.
+     *
+     * @param finalKey The final key obtained with via {@link #calcFinalKey()}.
+     * @param input The data to encrypt.
+     * @param output The output to write the encrypted data to.
+     *
+     * @throws IOException If there is an error reading the data.
+     */
+    protected void encryptDataRC4(byte[] finalKey, InputStream input, OutputStream output)
+            throws IOException
+    {
+        rc4.setKey(finalKey);
+        rc4.write(input, output);
+    }
+
+    /**
+     * Encrypt or decrypt data with RC4.
+     *
+     * @param finalKey The final key obtained with via {@link #calcFinalKey()}.
+     * @param input The data to encrypt.
+     * @param output The output to write the encrypted data to.
+     *
+     * @throws IOException If there is an error reading the data.
+     */
+    protected void encryptDataRC4(byte[] finalKey, byte[] input, OutputStream output) throws IOException
+    {
+        rc4.setKey(finalKey);
+        rc4.write(input, output);
+    }
+
+    
+    /**
      * Encrypt or decrypt data with AES with key length other than 256 bits.
      *
      * @param finalKey The final key obtained with via {@link #calcFinalKey()}.

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java?rev=1677425&r1=1677424&r2=1677425&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/encryption/StandardSecurityHandler.java Sun May  3 15:44:49 2015
@@ -587,8 +587,7 @@ public final class StandardSecurityHandl
 
         if( encRevision == 2 )
         {
-            rc4.setKey( rc4Key );
-            rc4.write( owner, result );
+            encryptDataRC4(rc4Key, owner, result);
         }
         else if( encRevision == 3 || encRevision == 4)
         {
@@ -603,9 +602,8 @@ public final class StandardSecurityHandl
                 {
                     iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
                 }
-                rc4.setKey( iterationKey );
                 result.reset();
-                rc4.write( otemp, result );
+                encryptDataRC4(iterationKey, otemp, result);
                 otemp = result.toByteArray();
             }
         }
@@ -765,8 +763,7 @@ public final class StandardSecurityHandl
         
         if( encRevision == 2 )
         {
-            rc4.setKey( encKey );
-            rc4.write( ENCRYPT_PADDING, result );
+            encryptDataRC4(encKey, ENCRYPT_PADDING, result );
         }
         else if( encRevision == 3 || encRevision == 4 )
         {
@@ -784,10 +781,9 @@ public final class StandardSecurityHandl
                 {
                     iterationKey[j] = (byte)(iterationKey[j] ^ i);
                 }
-                rc4.setKey( iterationKey );
-                ByteArrayInputStream input = new ByteArrayInputStream( result.toByteArray() );
+                ByteArrayInputStream input = new ByteArrayInputStream(result.toByteArray());
                 result.reset();
-                rc4.write( input, result );
+                encryptDataRC4(iterationKey, input, result);
             }
 
             byte[] finalResult = new byte[32];
@@ -822,9 +818,8 @@ public final class StandardSecurityHandl
         byte[] rc4Key = computeRC4key(ownerPassword, encRevision, length);
         byte[] paddedUser = truncateOrPad( userPassword );
 
-        rc4.setKey( rc4Key );
         ByteArrayOutputStream encrypted = new ByteArrayOutputStream();
-        rc4.write( new ByteArrayInputStream( paddedUser ), encrypted );
+        encryptDataRC4(rc4Key, new ByteArrayInputStream(paddedUser), encrypted);
 
         if( encRevision == 3 || encRevision == 4 )
         {
@@ -836,10 +831,9 @@ public final class StandardSecurityHandl
                 {
                     iterationKey[j] = (byte)(iterationKey[j] ^ (byte)i);
                 }
-                rc4.setKey( iterationKey );
                 ByteArrayInputStream input = new ByteArrayInputStream( encrypted.toByteArray() );
                 encrypted.reset();
-                rc4.write( input, encrypted );
+                encryptDataRC4(iterationKey, input, encrypted );
             }
         }