Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.encryption » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   
   18   package org.apache.pdfbox.pdmodel.encryption;
   19   
   20   import java.io.IOException;
   21   
   22   import org.apache.pdfbox.cos.COSArray;
   23   import org.apache.pdfbox.cos.COSDictionary;
   24   import org.apache.pdfbox.cos.COSName;
   25   import org.apache.pdfbox.cos.COSString;
   26   
   27   /**
   28    * This class is a specialized view of the encryption dictionary of a PDF document.
   29    * It contains a low level dictionary (COSDictionary) and provides the methods to
   30    * manage its fields.
   31    *
   32    * The available fields are the ones who are involved by standard security handler
   33    * and public key security handler.
   34    *
   35    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   36    * @author Benoit Guillon (benoit.guillon@snv.jussieu.fr)
   37    *
   38    * @version $Revision: 1.7 $
   39    */
   40   public class PDEncryptionDictionary
   41   {
   42       /**
   43        * See PDF Reference 1.4 Table 3.13.
   44        */
   45       public static final int VERSION0_UNDOCUMENTED_UNSUPPORTED = 0;
   46       /**
   47        * See PDF Reference 1.4 Table 3.13.
   48        */
   49       public static final int VERSION1_40_BIT_ALGORITHM = 1;
   50       /**
   51        * See PDF Reference 1.4 Table 3.13.
   52        */
   53       public static final int VERSION2_VARIABLE_LENGTH_ALGORITHM = 2;
   54       /**
   55        * See PDF Reference 1.4 Table 3.13.
   56        */
   57       public static final int VERSION3_UNPUBLISHED_ALGORITHM = 3;
   58       /**
   59        * See PDF Reference 1.4 Table 3.13.
   60        */
   61       public static final int VERSION4_SECURITY_HANDLER = 4;
   62   
   63       /**
   64        * The default security handler.
   65        */
   66       public static final String DEFAULT_NAME = "Standard";
   67   
   68       /**
   69        * The default length for the encryption key.
   70        */
   71       public static final int DEFAULT_LENGTH = 40;
   72   
   73       /**
   74        * The default version, according to the PDF Reference.
   75        */
   76       public static final int DEFAULT_VERSION = VERSION0_UNDOCUMENTED_UNSUPPORTED;
   77   
   78       /**
   79        * COS encryption dictionary.
   80        */
   81       protected COSDictionary encryptionDictionary = null;
   82   
   83       /**
   84        * creates a new empty encryption dictionary.
   85        */
   86       public PDEncryptionDictionary()
   87       {
   88           encryptionDictionary = new COSDictionary();
   89       }
   90   
   91       /**
   92        * creates a new encryption dictionary from the low level dictionary provided.
   93        * @param d the low level dictionary that will be managed by the newly created object
   94        */
   95       public PDEncryptionDictionary(COSDictionary d)
   96       {
   97           encryptionDictionary = d;
   98       }
   99   
  100       /**
  101        * This will get the dictionary associated with this encryption dictionary.
  102        *
  103        * @return The COS dictionary that this object wraps.
  104        */
  105       public COSDictionary getCOSDictionary()
  106       {
  107           return encryptionDictionary;
  108       }
  109   
  110       /**
  111        * Sets the filter entry of the encryption dictionary.
  112        *
  113        * @param filter The filter name.
  114        */
  115       public void setFilter(String filter)
  116       {
  117           encryptionDictionary.setItem( COSName.FILTER, COSName.getPDFName( filter ) );
  118       }
  119   
  120       /**
  121        * Get the name of the filter.
  122        *
  123        * @return The filter name contained in this encryption dictionary.
  124        */
  125       public String getFilter()
  126       {
  127           return encryptionDictionary.getNameAsString( COSName.FILTER );
  128       }
  129   
  130       /**
  131        * Set the subfilter entry of the encryption dictionary.
  132        *
  133        * @param subfilter The value of the subfilter field.
  134        */
  135       public void setSubFilter(String subfilter)
  136       {
  137           encryptionDictionary.setName( "SubFilter", subfilter );
  138       }
  139   
  140       /**
  141        * This will set the V entry of the encryption dictionary.<br /><br />
  142        * See PDF Reference 1.4 Table 3.13.  <br /><br/>
  143        * <b>Note: This value is used to decrypt the pdf document.  If you change this when
  144        * the document is encrypted then decryption will fail!.</b>
  145        *
  146        * @param version The new encryption version.
  147        */
  148       public void setVersion(int version)
  149       {
  150           encryptionDictionary.setInt( "V", version );
  151       }
  152   
  153       /**
  154        * This will return the V entry of the encryption dictionary.<br /><br />
  155        * See PDF Reference 1.4 Table 3.13.
  156        *
  157        * @return The encryption version to use.
  158        */
  159       public int getVersion()
  160       {
  161           return encryptionDictionary.getInt( "V", 0 );
  162       }
  163   
  164       /**
  165        * This will set the number of bits to use for the encryption algorithm.
  166        *
  167        * @param length The new key length.
  168        */
  169       public void setLength(int length)
  170       {
  171           encryptionDictionary.setInt("Length", length);
  172       }
  173   
  174       /**
  175        * This will return the Length entry of the encryption dictionary.<br /><br />
  176        * The length in <b>bits</b> for the encryption algorithm.  This will return a multiple of 8.
  177        *
  178        * @return The length in bits for the encryption algorithm
  179        */
  180       public int getLength()
  181       {
  182           return encryptionDictionary.getInt( "Length", 40 );
  183       }
  184   
  185       /**
  186        * This will set the R entry of the encryption dictionary.<br /><br />
  187        * See PDF Reference 1.4 Table 3.14.  <br /><br/>
  188        *
  189        * <b>Note: This value is used to decrypt the pdf document.  If you change this when
  190        * the document is encrypted then decryption will fail!.</b>
  191        *
  192        * @param revision The new encryption version.
  193        */
  194       public void setRevision(int revision)
  195       {
  196           encryptionDictionary.setInt( "R", revision );
  197       }
  198   
  199       /**
  200        * This will return the R entry of the encryption dictionary.<br /><br />
  201        * See PDF Reference 1.4 Table 3.14.
  202        *
  203        * @return The encryption revision to use.
  204        */
  205       public int getRevision()
  206       {
  207           return encryptionDictionary.getInt( "R", DEFAULT_VERSION );
  208       }
  209   
  210        /**
  211        * This will set the O entry in the standard encryption dictionary.
  212        *
  213        * @param o A 32 byte array or null if there is no owner key.
  214        *
  215        * @throws IOException If there is an error setting the data.
  216        */
  217       public void setOwnerKey(byte[] o) throws IOException
  218       {
  219           COSString owner = new COSString();
  220           owner.append( o );
  221           encryptionDictionary.setItem( COSName.getPDFName( "O" ), owner );
  222       }
  223   
  224       /**
  225        * This will get the O entry in the standard encryption dictionary.
  226        *
  227        * @return A 32 byte array or null if there is no owner key.
  228        *
  229        * @throws IOException If there is an error accessing the data.
  230        */
  231       public byte[] getOwnerKey() throws IOException
  232       {
  233           byte[] o = null;
  234           COSString owner = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "O" ) );
  235           if( owner != null )
  236           {
  237               o = owner.getBytes();
  238           }
  239           return o;
  240       }
  241   
  242       /**
  243        * This will set the U entry in the standard encryption dictionary.
  244        *
  245        * @param u A 32 byte array.
  246        *
  247        * @throws IOException If there is an error setting the data.
  248        */
  249       public void setUserKey(byte[] u) throws IOException
  250       {
  251           COSString user = new COSString();
  252           user.append( u );
  253           encryptionDictionary.setItem( COSName.getPDFName( "U" ), user );
  254       }
  255   
  256       /**
  257        * This will get the U entry in the standard encryption dictionary.
  258        *
  259        * @return A 32 byte array or null if there is no user key.
  260        *
  261        * @throws IOException If there is an error accessing the data.
  262        */
  263       public byte[] getUserKey() throws IOException
  264       {
  265           byte[] u = null;
  266           COSString user = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "U" ) );
  267           if( user != null )
  268           {
  269               u = user.getBytes();
  270           }
  271           return u;
  272       }
  273   
  274       /**
  275        * This will set the permissions bit mask.
  276        *
  277        * @param permissions The new permissions bit mask
  278        */
  279       public void setPermissions(int permissions)
  280       {
  281           encryptionDictionary.setInt( "P", permissions );
  282       }
  283   
  284       /**
  285        * This will get the permissions bit mask.
  286        *
  287        * @return The permissions bit mask.
  288        */
  289       public int getPermissions()
  290       {
  291           return encryptionDictionary.getInt( "P", 0 );
  292       }
  293   
  294       /**
  295        * This will set the Recipients field of the dictionary. This field contains an array
  296        * of string.
  297        * @param recipients the array of bytes arrays to put in the Recipients field.
  298        * @throws IOException If there is an error setting the data.
  299        */
  300       public void setRecipients(byte[][] recipients) throws IOException
  301       {
  302           COSArray array = new COSArray();
  303           for(int i=0; i<recipients.length; i++)
  304           {
  305               COSString recip = new COSString();
  306               recip.append(recipients[i]);
  307               recip.setForceLiteralForm(true);
  308               array.add(recip);
  309           }
  310           encryptionDictionary.setItem(COSName.getPDFName("Recipients"), array);
  311       }
  312   
  313       /**
  314        * Returns the number of recipients contained in the Recipients field of the dictionary.
  315        *
  316        * @return the number of recipients contained in the Recipients field.
  317        */
  318       public int getRecipientsLength()
  319       {
  320           COSArray array = (COSArray)encryptionDictionary.getItem(COSName.getPDFName("Recipients"));
  321           return array.size();
  322       }
  323   
  324       /**
  325        * returns the COSString contained in the Recipients field at position i.
  326        *
  327        * @param i the position in the Recipients field array.
  328        *
  329        * @return a COSString object containing information about the recipient number i.
  330        */
  331       public COSString getRecipientStringAt(int i)
  332       {
  333           COSArray array = (COSArray)encryptionDictionary.getItem(COSName.getPDFName("Recipients"));
  334           return (COSString)array.get(i);
  335       }
  336   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.encryption » [javadoc | source]