KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > Encrypt


1 /**
2  * Copyright (c) 2004, 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;
32
33 import java.io.FileInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.security.cert.CertificateFactory JavaDoc;
36 import java.security.cert.X509Certificate JavaDoc;
37
38 import org.pdfbox.pdmodel.PDDocument;
39 import org.pdfbox.pdmodel.encryption.AccessPermission;
40 import org.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy;
41 import org.pdfbox.pdmodel.encryption.PublicKeyRecipient;
42 import org.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
43
44 /**
45  * This will read a document from the filesystem, encrypt it and and then write
46  * the results to the filesystem. <br/><br/>
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.8 $
50  */

51 public class Encrypt
52 {
53
54     /**
55      * This is the entry point for the application.
56      *
57      * @param args The command-line arguments.
58      *
59      * @throws Exception If there is an error decrypting the document.
60      */

61     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
62     {
63         Encrypt encrypt = new Encrypt();
64         encrypt.encrypt( args );
65     }
66
67     private void encrypt( String JavaDoc[] args ) throws Exception JavaDoc
68     {
69         if( args.length < 1 )
70         {
71             usage();
72         }
73         else
74         {
75             AccessPermission ap = new AccessPermission();
76             
77             String JavaDoc infile = null;
78             String JavaDoc outfile = null;
79             String JavaDoc certFile = null;
80             String JavaDoc userPassword = "";
81             String JavaDoc ownerPassword = "";
82             
83             int keyLength = 48;
84
85             PDDocument document = null;
86
87             try
88             {
89                 for( int i=0; i<args.length; i++ )
90                 {
91                     String JavaDoc key = args[i];
92                     if( key.equals( "-O" ) )
93                     {
94                         ownerPassword = args[++i];
95                     }
96                     else if( key.equals( "-U" ) )
97                     {
98                         userPassword = args[++i];
99                     }
100                     else if( key.equals( "-canAssemble" ) )
101                     {
102                         ap.setCanAssembleDocument(args[++i].equalsIgnoreCase( "true" ));
103                     }
104                     else if( key.equals( "-canExtractContent" ) )
105                     {
106                         ap.setCanExtractContent( args[++i].equalsIgnoreCase( "true" ) );
107                     }
108                     else if( key.equals( "-canExtractForAccessibility" ) )
109                     {
110                         ap.setCanExtractForAccessibility( args[++i].equalsIgnoreCase( "true" ) );
111                     }
112                     else if( key.equals( "-canFillInForm" ) )
113                     {
114                         ap.setCanFillInForm( args[++i].equalsIgnoreCase( "true" ) );
115                     }
116                     else if( key.equals( "-canModify" ) )
117                     {
118                         ap.setCanModify( args[++i].equalsIgnoreCase( "true" ) );
119                     }
120                     else if( key.equals( "-canModifyAnnotations" ) )
121                     {
122                         ap.setCanModifyAnnotations( args[++i].equalsIgnoreCase( "true" ) );
123                     }
124                     else if( key.equals( "-canPrint" ) )
125                     {
126                         ap.setCanPrint( args[++i].equalsIgnoreCase( "true" ) );
127                     }
128                     else if( key.equals( "-canPrintDegraded" ) )
129                     {
130                         ap.setCanPrintDegraded( args[++i].equalsIgnoreCase( "true" ) );
131                     }
132                     else if( key.equals( "-certFile" ) )
133                     {
134                         certFile = args[++i];
135                     }
136                     else if( key.equals( "-keyLength" ) )
137                     {
138                         try
139                         {
140                             keyLength = Integer.parseInt( args[++i] );
141                         }
142                         catch( NumberFormatException JavaDoc e )
143                         {
144                             throw new NumberFormatException JavaDoc(
145                                 "Error: -keyLength is not an integer '" + args[i] + "'" );
146                         }
147                     }
148                     else if( infile == null )
149                     {
150                         infile = key;
151                     }
152                     else if( outfile == null )
153                     {
154                         outfile = key;
155                     }
156                     else
157                     {
158                         usage();
159                     }
160                 }
161                 if( infile == null )
162                 {
163                     usage();
164                 }
165                 if( outfile == null )
166                 {
167                     outfile = infile;
168                 }
169                 document = PDDocument.load( infile );
170
171                 if( !document.isEncrypted() )
172                 {
173                     if( certFile != null )
174                     {
175                         PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
176                         PublicKeyRecipient recip = new PublicKeyRecipient();
177                         recip.setPermission(ap);
178                         
179                         
180                         CertificateFactory JavaDoc cf = CertificateFactory.getInstance("X.509");
181                         InputStream JavaDoc inStream = new FileInputStream JavaDoc(certFile);
182                         X509Certificate JavaDoc certificate = (X509Certificate JavaDoc)cf.generateCertificate(inStream);
183                         inStream.close();
184                         
185                         recip.setX509(certificate);
186                         
187                         ppp.addRecipient(recip);
188                         
189                         ppp.setEncryptionKeyLength(keyLength);
190                         
191                         document.protect(ppp);
192                     }
193                     else
194                     {
195                         StandardProtectionPolicy spp =
196                             new StandardProtectionPolicy(ownerPassword, userPassword, ap);
197                         spp.setEncryptionKeyLength(keyLength);
198                         document.protect(spp);
199                     }
200                     document.save( outfile );
201                 }
202                 else
203                 {
204                     System.err.println( "Error: Document is already encrypted." );
205                 }
206             }
207             finally
208             {
209                 if( document != null )
210                 {
211                     document.close();
212                 }
213             }
214         }
215     }
216
217     /**
218      * This will print a usage message.
219      */

220     private static void usage()
221     {
222         System.err.println( "usage: java org.pdfbox.Encrypt [options] <inputfile> [outputfile]" );
223         System.err.println( " -O <password> " +
224                                             "Set the owner password(ignored if cert is set)" );
225         System.err.println( " -U <password> " +
226                                             "Set the user password(ignored if cert is set)" );
227         System.err.println( " -certFile <path to cert> Path to X.509 certificate" );
228         System.err.println( " -canAssemble <true|false> Set the assemble permission" );
229         System.err.println( " -canExtractContent <true|false> Set the extraction permission" );
230         System.err.println( " -canExtractForAccessibility <true|false> Set the extraction permission" );
231         System.err.println( " -canFillInForm <true|false> Set the fill in form permission" );
232         System.err.println( " -canModify <true|false> Set the modify permission" );
233         System.err.println( " -canModifyAnnotations <true|false> Set the modify annots permission" );
234         System.err.println( " -canPrint <true|false> Set the print permission" );
235         System.err.println( " -canPrintDegraded <true|false> Set the print degraded permission" );
236         System.err.println( " -keyLength <length> The length of the key in bits(40)" );
237         System.err.println( "\nNote: By default all permissions are set to true!" );
238         System.exit( 1 );
239     }
240
241 }
Popular Tags