KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > Decrypt


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 package org.pdfbox;
32
33 import java.io.FileInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.security.KeyStore JavaDoc;
36
37 import org.pdfbox.pdmodel.PDDocument;
38 import org.pdfbox.pdmodel.encryption.AccessPermission;
39 import org.pdfbox.pdmodel.encryption.DecryptionMaterial;
40 import org.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial;
41 import org.pdfbox.pdmodel.encryption.StandardDecryptionMaterial;
42
43 /**
44  * This will read a document from the filesystem, decrypt it and and then write
45  * the results to the filesystem. <br/><br/>
46  *
47  * usage: java org.pdfbox.Decrypt &lt;password&gt; &lt;inputfile&gt; &lt;outputfile&gt;
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @version $Revision: 1.5 $
51  */

52 public class Decrypt
53 {
54     private static final String JavaDoc ALIAS = "-alias";
55     private static final String JavaDoc PASSWORD = "-password";
56     private static final String JavaDoc KEYSTORE = "-keyStore";
57     
58     
59     /**
60      * This is the entry point for the application.
61      *
62      * @param args The command-line arguments.
63      *
64      * @throws Exception If there is an error decrypting the document.
65      */

66     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
67     {
68         Decrypt decrypt = new Decrypt();
69         decrypt.decrypt( args );
70     }
71
72     private void decrypt( String JavaDoc[] args ) throws Exception JavaDoc
73     {
74         if( args.length < 2 || args.length > 3 )
75         {
76             usage();
77         }
78         else
79         {
80             String JavaDoc password = null;
81             String JavaDoc infile = null;
82             String JavaDoc outfile = null;
83             String JavaDoc alias = null;
84             String JavaDoc keyStore = null;
85             for( int i=0; i<args.length; i++ )
86             {
87                 if( args[i].equals( ALIAS ) )
88                 {
89                     i++;
90                     if( i >= args.length )
91                     {
92                         usage();
93                     }
94                     alias = args[i];
95                 }
96                 else if( args[i].equals( KEYSTORE ) )
97                 {
98                     i++;
99                     if( i >= args.length )
100                     {
101                         usage();
102                     }
103                     keyStore = args[i];
104                 }
105                 else if( args[i].equals( PASSWORD ) )
106                 {
107                     i++;
108                     if( i >= args.length )
109                     {
110                         usage();
111                     }
112                     password = args[i];
113                 }
114                 else if( infile == null )
115                 {
116                     infile = args[i];
117                 }
118                 else if( outfile == null )
119                 {
120                     outfile = args[i];
121                 }
122                 else
123                 {
124                     usage();
125                 }
126             }
127             if( infile == null )
128             {
129                 usage();
130             }
131             if( outfile == null )
132             {
133                 outfile = infile;
134             }
135             if( password == null )
136             {
137                 password = "";
138             }
139             
140
141             PDDocument document = null;
142
143             try
144             {
145                 document = PDDocument.load( infile );
146
147                 if( document.isEncrypted() )
148                 {
149                     DecryptionMaterial decryptionMaterial = null;
150                     if( keyStore != null )
151                     {
152                         KeyStore JavaDoc ks = KeyStore.getInstance("PKCS12");
153                         ks.load(new FileInputStream JavaDoc(keyStore), password.toCharArray());
154                             
155                         decryptionMaterial = new PublicKeyDecryptionMaterial(ks, alias, password);
156                     }
157                     else
158                     {
159                         decryptionMaterial = new StandardDecryptionMaterial(password);
160                     }
161                     document.openProtection(decryptionMaterial);
162                     AccessPermission ap = document.getCurrentAccessPermission();
163                     if(ap.isOwnerPermission())
164                     {
165                         document.save( outfile );
166                     }
167                     else
168                     {
169                         throw new IOException JavaDoc(
170                         "Error: You are only allowed to decrypt a document with the owner password." );
171                     }
172                 }
173                 else
174                 {
175                     System.err.println( "Error: Document is not encrypted." );
176                 }
177             }
178             finally
179             {
180                 if( document != null )
181                 {
182                     document.close();
183                 }
184             }
185         }
186     }
187
188     /**
189      * This will print a usage message.
190      */

191     private static void usage()
192     {
193         System.err.println( "usage: java org.pdfbox.Decrypt " +
194                             "[options] <inputfile> [outputfile]" );
195         System.err.println( "-alias The alias of the key in the certificate file " +
196                                          "(mandatory if several keys are available)");
197         System.err.println( "-password The password to open the certificate and extract the private key from it." );
198         System.err.println( "-keyStore The KeyStore that holds the certificate." );
199         System.exit( -1 );
200     }
201
202 }
Popular Tags