KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > encryption > PDEncryptionDictionary


1 /**
2  * Copyright (c) 2003-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31
32 package org.pdfbox.pdmodel.encryption;
33
34 import java.io.IOException JavaDoc;
35
36 import org.pdfbox.cos.COSArray;
37 import org.pdfbox.cos.COSDictionary;
38 import org.pdfbox.cos.COSName;
39 import org.pdfbox.cos.COSString;
40
41 /**
42  * This class is a specialized view of the encryption dictionary of a PDF document.
43  * It contains a low level dictionary (COSDictionary) and provides the methods to
44  * manage its fields.
45  *
46  * The available fields are the ones who are involved by standard security handler
47  * and public key security handler.
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @author Benoit Guillon (benoit.guillon@snv.jussieu.fr)
51  *
52  * @version $Revision: 1.7 $
53  */

54 public class PDEncryptionDictionary
55 {
56     /**
57      * See PDF Reference 1.4 Table 3.13.
58      */

59     public static final int VERSION0_UNDOCUMENTED_UNSUPPORTED = 0;
60     /**
61      * See PDF Reference 1.4 Table 3.13.
62      */

63     public static final int VERSION1_40_BIT_ALGORITHM = 1;
64     /**
65      * See PDF Reference 1.4 Table 3.13.
66      */

67     public static final int VERSION2_VARIABLE_LENGTH_ALGORITHM = 2;
68     /**
69      * See PDF Reference 1.4 Table 3.13.
70      */

71     public static final int VERSION3_UNPUBLISHED_ALGORITHM = 3;
72     /**
73      * See PDF Reference 1.4 Table 3.13.
74      */

75     public static final int VERSION4_SECURITY_HANDLER = 4;
76
77     /**
78      * The default security handler.
79      */

80     public static final String JavaDoc DEFAULT_NAME = "Standard";
81
82     /**
83      * The default length for the encryption key.
84      */

85     public static final int DEFAULT_LENGTH = 40;
86
87     /**
88      * The default version, according to the PDF Reference.
89      */

90     public static final int DEFAULT_VERSION = VERSION0_UNDOCUMENTED_UNSUPPORTED;
91     
92     /**
93      * COS encryption dictionary.
94      */

95     protected COSDictionary encryptionDictionary = null;
96     
97     /**
98      * creates a new empty encryption dictionary.
99      */

100     public PDEncryptionDictionary()
101     {
102         encryptionDictionary = new COSDictionary();
103     }
104     
105     /**
106      * creates a new encryption dictionary from the low level dictionary provided.
107      * @param d the low level dictionary that will be managed by the newly created object
108      */

109     public PDEncryptionDictionary(COSDictionary d)
110     {
111         encryptionDictionary = d;
112     }
113     
114     /**
115      * This will get the dictionary associated with this encryption dictionary.
116      *
117      * @return The COS dictionary that this object wraps.
118      */

119     public COSDictionary getCOSDictionary()
120     {
121         return encryptionDictionary;
122     }
123     
124     /**
125      * Sets the filter entry of the encryption dictionary.
126      *
127      * @param filter The filter name.
128      */

129     public void setFilter(String JavaDoc filter)
130     {
131         encryptionDictionary.setItem( COSName.FILTER, COSName.getPDFName( filter ) );
132     }
133     
134     /**
135      * Get the name of the filter.
136      *
137      * @return The filter name contained in this encryption dictionary.
138      */

139     public String JavaDoc getFilter()
140     {
141         return encryptionDictionary.getNameAsString( COSName.FILTER );
142     }
143     
144     /**
145      * Set the subfilter entry of the encryption dictionary.
146      *
147      * @param subfilter The value of the subfilter field.
148      */

149     public void setSubFilter(String JavaDoc subfilter)
150     {
151         encryptionDictionary.setName( "SubFilter", subfilter );
152     }
153     
154     /**
155      * This will set the V entry of the encryption dictionary.<br /><br />
156      * See PDF Reference 1.4 Table 3.13. <br /><br/>
157      * <b>Note: This value is used to decrypt the pdf document. If you change this when
158      * the document is encrypted then decryption will fail!.</b>
159      *
160      * @param version The new encryption version.
161      */

162     public void setVersion(int version)
163     {
164         encryptionDictionary.setInt( "V", version );
165     }
166     
167     /**
168      * This will return the V entry of the encryption dictionary.<br /><br />
169      * See PDF Reference 1.4 Table 3.13.
170      *
171      * @return The encryption version to use.
172      */

173     public int getVersion()
174     {
175         return encryptionDictionary.getInt( "V", 0 );
176     }
177     
178     /**
179      * This will set the number of bits to use for the encryption algorithm.
180      *
181      * @param length The new key length.
182      */

183     public void setLength(int length)
184     {
185         encryptionDictionary.setInt("Length", length);
186     }
187     
188     /**
189      * This will return the Length entry of the encryption dictionary.<br /><br />
190      * The length in <b>bits</b> for the encryption algorithm. This will return a multiple of 8.
191      *
192      * @return The length in bits for the encryption algorithm
193      */

194     public int getLength()
195     {
196         return encryptionDictionary.getInt( "Length", 40 );
197     }
198     
199     /**
200      * This will set the R entry of the encryption dictionary.<br /><br />
201      * See PDF Reference 1.4 Table 3.14. <br /><br/>
202      *
203      * <b>Note: This value is used to decrypt the pdf document. If you change this when
204      * the document is encrypted then decryption will fail!.</b>
205      *
206      * @param revision The new encryption version.
207      */

208     public void setRevision(int revision)
209     {
210         encryptionDictionary.setInt( "R", revision );
211     }
212     
213     /**
214      * This will return the R entry of the encryption dictionary.<br /><br />
215      * See PDF Reference 1.4 Table 3.14.
216      *
217      * @return The encryption revision to use.
218      */

219     public int getRevision()
220     {
221         return encryptionDictionary.getInt( "R", DEFAULT_VERSION );
222     }
223     
224      /**
225      * This will set the O entry in the standard encryption dictionary.
226      *
227      * @param o A 32 byte array or null if there is no owner key.
228      *
229      * @throws IOException If there is an error setting the data.
230      */

231     public void setOwnerKey(byte[] o) throws IOException JavaDoc
232     {
233         COSString owner = new COSString();
234         owner.append( o );
235         encryptionDictionary.setItem( COSName.getPDFName( "O" ), owner );
236     }
237     
238     /**
239      * This will get the O entry in the standard encryption dictionary.
240      *
241      * @return A 32 byte array or null if there is no owner key.
242      *
243      * @throws IOException If there is an error accessing the data.
244      */

245     public byte[] getOwnerKey() throws IOException JavaDoc
246     {
247         byte[] o = null;
248         COSString owner = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "O" ) );
249         if( owner != null )
250         {
251             o = owner.getBytes();
252         }
253         return o;
254     }
255     
256     /**
257      * This will set the U entry in the standard encryption dictionary.
258      *
259      * @param u A 32 byte array.
260      *
261      * @throws IOException If there is an error setting the data.
262      */

263     public void setUserKey(byte[] u) throws IOException JavaDoc
264     {
265         COSString user = new COSString();
266         user.append( u );
267         encryptionDictionary.setItem( COSName.getPDFName( "U" ), user );
268     }
269     
270     /**
271      * This will get the U entry in the standard encryption dictionary.
272      *
273      * @return A 32 byte array or null if there is no user key.
274      *
275      * @throws IOException If there is an error accessing the data.
276      */

277     public byte[] getUserKey() throws IOException JavaDoc
278     {
279         byte[] u = null;
280         COSString user = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "U" ) );
281         if( user != null )
282         {
283             u = user.getBytes();
284         }
285         return u;
286     }
287     
288     /**
289      * This will set the permissions bit mask.
290      *
291      * @param permissions The new permissions bit mask
292      */

293     public void setPermissions(int permissions)
294     {
295         encryptionDictionary.setInt( "P", permissions );
296     }
297     
298     /**
299      * This will get the permissions bit mask.
300      *
301      * @return The permissions bit mask.
302      */

303     public int getPermissions()
304     {
305         return encryptionDictionary.getInt( "P", 0 );
306     }
307     
308     /**
309      * This will set the Recipients field of the dictionary. This field contains an array
310      * of string.
311      * @param recipients the array of bytes arrays to put in the Recipients field.
312      * @throws IOException If there is an error setting the data.
313      */

314     public void setRecipients(byte[][] recipients) throws IOException JavaDoc
315     {
316         COSArray array = new COSArray();
317         for(int i=0; i<recipients.length; i++)
318         {
319             COSString recip = new COSString();
320             recip.append(recipients[i]);
321             recip.setForceLiteralForm(true);
322             array.add(recip);
323         }
324         encryptionDictionary.setItem(COSName.getPDFName("Recipients"), array);
325     }
326     
327     /**
328      * Returns the number of recipients contained in the Recipients field of the dictionary.
329      *
330      * @return the number of recipients contained in the Recipients field.
331      */

332     public int getRecipientsLength()
333     {
334         COSArray array = (COSArray)encryptionDictionary.getItem(COSName.getPDFName("Recipients"));
335         return array.size();
336     }
337     
338     /**
339      * returns the COSString contained in the Recipients field at position i.
340      *
341      * @param i the position in the Recipients field array.
342      *
343      * @return a COSString object containing information about the recipient number i.
344      */

345     public COSString getRecipientStringAt(int i)
346     {
347         COSArray array = (COSArray)encryptionDictionary.getItem(COSName.getPDFName("Recipients"));
348         return (COSString)array.get(i);
349     }
350 }
Popular Tags