KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2003, 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 package org.pdfbox.pdmodel.encryption;
32
33 import org.pdfbox.cos.COSDictionary;
34 import org.pdfbox.cos.COSInteger;
35 import org.pdfbox.cos.COSName;
36 import org.pdfbox.cos.COSNumber;
37 import org.pdfbox.cos.COSString;
38
39 import java.io.IOException JavaDoc;
40
41 /**
42  * This class holds information that is related to the standard PDF encryption.
43  *
44  * See PDF Reference 1.4 section "3.5 Encryption"
45  *
46  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
47  * @version $Revision: 1.7 $
48  * @deprecated Made deprecated by the new security layer of PDFBox. Use SecurityHandlers instead.
49  */

50 public class PDStandardEncryption extends PDEncryptionDictionary
51 {
52     /**
53      * The 'Filter' name for this security handler.
54      */

55     public static final String JavaDoc FILTER_NAME = "Standard";
56
57     /**
58      * The default revision of one is not specified.
59      */

60     public static final int DEFAULT_REVISION = 3;
61
62     /**
63      * Encryption revision 2.
64      */

65     public static final int REVISION2 = 2;
66     /**
67      * Encryption revision 3.
68      */

69     public static final int REVISION3 = 3;
70     /**
71      * Encryption revision 4.
72      */

73     public static final int REVISION4 = 4;
74
75     /**
76      * The default set of permissions which is to allow all.
77      */

78     public static final int DEFAULT_PERMISSIONS = 0xFFFFFFFF ^ 3;//bits 0 & 1 need to be zero
79

80     private static final int PRINT_BIT = 3;
81     private static final int MODIFICATION_BIT = 4;
82     private static final int EXTRACT_BIT = 5;
83     private static final int MODIFY_ANNOTATIONS_BIT = 6;
84     private static final int FILL_IN_FORM_BIT = 9;
85     private static final int EXTRACT_FOR_ACCESSIBILITY_BIT = 10;
86     private static final int ASSEMBLE_DOCUMENT_BIT = 11;
87     private static final int DEGRADED_PRINT_BIT = 12;
88
89     /**
90      * Default constructor that uses Version 2, Revision 3, 40 bit encryption,
91      * all permissions allowed.
92      */

93     public PDStandardEncryption()
94     {
95         super();
96         encryptionDictionary.setItem( COSName.FILTER, COSName.getPDFName( FILTER_NAME ) );
97         setVersion( PDEncryptionDictionary.VERSION1_40_BIT_ALGORITHM );
98         setRevision( PDStandardEncryption.REVISION2 );
99         setPermissions( DEFAULT_PERMISSIONS );
100     }
101
102     /**
103      * Constructor from existing dictionary.
104      *
105      * @param dict The existing encryption dictionary.
106      */

107     public PDStandardEncryption( COSDictionary dict )
108     {
109         super( dict );
110     }
111
112     /**
113      * This will return the R entry of the encryption dictionary.<br /><br />
114      * See PDF Reference 1.4 Table 3.14.
115      *
116      * @return The encryption revision to use.
117      */

118     public int getRevision()
119     {
120         int revision = DEFAULT_VERSION;
121         COSNumber cosRevision = (COSNumber)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "R" ) );
122         if( cosRevision != null )
123         {
124             revision = cosRevision.intValue();
125         }
126         return revision;
127     }
128
129     /**
130      * This will set the R entry of the encryption dictionary.<br /><br />
131      * See PDF Reference 1.4 Table 3.14. <br /><br/>
132      *
133      * <b>Note: This value is used to decrypt the pdf document. If you change this when
134      * the document is encrypted then decryption will fail!.</b>
135      *
136      * @param revision The new encryption version.
137      */

138     public void setRevision( int revision )
139     {
140         encryptionDictionary.setItem( COSName.getPDFName( "R" ), new COSInteger( revision ) );
141     }
142
143     /**
144      * This will get the O entry in the standard encryption dictionary.
145      *
146      * @return A 32 byte array or null if there is no owner key.
147      */

148     public byte[] getOwnerKey()
149     {
150        byte[] o = null;
151        COSString owner = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "O" ) );
152        if( owner != null )
153        {
154            o = owner.getBytes();
155        }
156        return o;
157     }
158
159     /**
160      * This will set the O entry in the standard encryption dictionary.
161      *
162      * @param o A 32 byte array or null if there is no owner key.
163      *
164      * @throws IOException If there is an error setting the data.
165      */

166     public void setOwnerKey( byte[] o ) throws IOException JavaDoc
167     {
168        COSString owner = new COSString();
169        owner.append( o );
170        encryptionDictionary.setItem( COSName.getPDFName( "O" ), owner );
171     }
172
173     /**
174      * This will get the U entry in the standard encryption dictionary.
175      *
176      * @return A 32 byte array or null if there is no user key.
177      */

178     public byte[] getUserKey()
179     {
180        byte[] u = null;
181        COSString user = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "U" ) );
182        if( user != null )
183        {
184            u = user.getBytes();
185        }
186        return u;
187     }
188
189     /**
190      * This will set the U entry in the standard encryption dictionary.
191      *
192      * @param u A 32 byte array.
193      *
194      * @throws IOException If there is an error setting the data.
195      */

196     public void setUserKey( byte[] u ) throws IOException JavaDoc
197     {
198        COSString user = new COSString();
199        user.append( u );
200        encryptionDictionary.setItem( COSName.getPDFName( "U" ), user );
201     }
202
203     /**
204      * This will get the permissions bit mask.
205      *
206      * @return The permissions bit mask.
207      */

208     public int getPermissions()
209     {
210         int permissions = 0;
211         COSInteger p = (COSInteger)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "P" ) );
212         if( p != null )
213         {
214             permissions = p.intValue();
215         }
216         return permissions;
217     }
218
219     /**
220      * This will set the permissions bit mask.
221      *
222      * @param p The new permissions bit mask
223      */

224     public void setPermissions( int p )
225     {
226         encryptionDictionary.setItem( COSName.getPDFName( "P" ), new COSInteger( p ) );
227     }
228
229     private boolean isPermissionBitOn( int bit )
230     {
231         return (getPermissions() & (1 << (bit-1))) != 0;
232     }
233
234     private boolean setPermissionBit( int bit, boolean value )
235     {
236         int permissions = getPermissions();
237         if( value )
238         {
239             permissions = permissions | (1 << (bit-1));
240         }
241         else
242         {
243             permissions = permissions & (0xFFFFFFFF ^ (1 << (bit-1)));
244         }
245         setPermissions( permissions );
246
247         return (getPermissions() & (1 << (bit-1))) != 0;
248     }
249
250     /**
251      * This will tell if the user can print.
252      *
253      * @return true If supplied with the user password they are allowed to print.
254      */

255     public boolean canPrint()
256     {
257         return isPermissionBitOn( PRINT_BIT );
258     }
259
260     /**
261      * Set if the user can print.
262      *
263      * @param allowPrinting A boolean determining if the user can print.
264      */

265     public void setCanPrint( boolean allowPrinting )
266     {
267         setPermissionBit( PRINT_BIT, allowPrinting );
268     }
269
270     /**
271      * This will tell if the user can modify contents of the document.
272      *
273      * @return true If supplied with the user password they are allowed to modify the document
274      */

275     public boolean canModify()
276     {
277         return isPermissionBitOn( MODIFICATION_BIT );
278     }
279
280     /**
281      * Set if the user can modify the document.
282      *
283      * @param allowModifications A boolean determining if the user can modify the document.
284      */

285     public void setCanModify( boolean allowModifications )
286     {
287         setPermissionBit( MODIFICATION_BIT, allowModifications );
288     }
289
290     /**
291      * This will tell if the user can extract text and images from the PDF document.
292      *
293      * @return true If supplied with the user password they are allowed to extract content
294      * from the PDF document
295      */

296     public boolean canExtractContent()
297     {
298         return isPermissionBitOn( EXTRACT_BIT );
299     }
300
301     /**
302      * Set if the user can extract content from the document.
303      *
304      * @param allowExtraction A boolean determining if the user can extract content
305      * from the document.
306      */

307     public void setCanExtractContent( boolean allowExtraction )
308     {
309         setPermissionBit( EXTRACT_BIT, allowExtraction );
310     }
311
312     /**
313      * This will tell if the user can add/modify text annotations, fill in interactive forms fields.
314      *
315      * @return true If supplied with the user password they are allowed to modify annotations.
316      */

317     public boolean canModifyAnnotations()
318     {
319         return isPermissionBitOn( MODIFY_ANNOTATIONS_BIT );
320     }
321
322     /**
323      * Set if the user can modify annotations.
324      *
325      * @param allowAnnotationModification A boolean determining if the user can modify annotations.
326      */

327     public void setCanModifyAnnotations( boolean allowAnnotationModification )
328     {
329         setPermissionBit( MODIFY_ANNOTATIONS_BIT, allowAnnotationModification );
330     }
331
332     /**
333      * This will tell if the user can fill in interactive forms.
334      *
335      * @return true If supplied with the user password they are allowed to fill in form fields.
336      */

337     public boolean canFillInForm()
338     {
339         return isPermissionBitOn( FILL_IN_FORM_BIT );
340     }
341
342     /**
343      * Set if the user can fill in interactive forms.
344      *
345      * @param allowFillingInForm A boolean determining if the user can fill in interactive forms.
346      */

347     public void setCanFillInForm( boolean allowFillingInForm )
348     {
349         setPermissionBit( FILL_IN_FORM_BIT, allowFillingInForm );
350     }
351
352     /**
353      * This will tell if the user can extract text and images from the PDF document
354      * for accessibility purposes.
355      *
356      * @return true If supplied with the user password they are allowed to extract content
357      * from the PDF document
358      */

359     public boolean canExtractForAccessibility()
360     {
361         return isPermissionBitOn( EXTRACT_FOR_ACCESSIBILITY_BIT );
362     }
363
364     /**
365      * Set if the user can extract content from the document for accessibility purposes.
366      *
367      * @param allowExtraction A boolean determining if the user can extract content
368      * from the document.
369      */

370     public void setCanExtractForAccessibility( boolean allowExtraction )
371     {
372         setPermissionBit( EXTRACT_FOR_ACCESSIBILITY_BIT, allowExtraction );
373     }
374
375     /**
376      * This will tell if the user can insert/rotate/delete pages.
377      *
378      * @return true If supplied with the user password they are allowed to extract content
379      * from the PDF document
380      */

381     public boolean canAssembleDocument()
382     {
383         return isPermissionBitOn( ASSEMBLE_DOCUMENT_BIT );
384     }
385
386     /**
387      * Set if the user can insert/rotate/delete pages.
388      *
389      * @param allowAssembly A boolean determining if the user can assemble the document.
390      */

391     public void setCanAssembleDocument( boolean allowAssembly )
392     {
393         setPermissionBit( ASSEMBLE_DOCUMENT_BIT, allowAssembly );
394     }
395
396     /**
397      * This will tell if the user can print the document in a degraded format.
398      *
399      * @return true If supplied with the user password they are allowed to print the
400      * document in a degraded format.
401      */

402     public boolean canPrintDegraded()
403     {
404         return isPermissionBitOn( DEGRADED_PRINT_BIT );
405     }
406
407     /**
408      * Set if the user can print the document in a degraded format.
409      *
410      * @param allowAssembly A boolean determining if the user can print the
411      * document in a degraded format.
412      */

413     public void setCanPrintDegraded( boolean allowAssembly )
414     {
415         setPermissionBit( DEGRADED_PRINT_BIT, allowAssembly );
416     }
417 }
Popular Tags