001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox;
031:
032: import java.io.FileInputStream;
033: import java.io.InputStream;
034: import java.security.cert.CertificateFactory;
035: import java.security.cert.X509Certificate;
036:
037: import org.pdfbox.pdmodel.PDDocument;
038: import org.pdfbox.pdmodel.encryption.AccessPermission;
039: import org.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy;
040: import org.pdfbox.pdmodel.encryption.PublicKeyRecipient;
041: import org.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
042:
043: /**
044: * This will read a document from the filesystem, encrypt it and and then write
045: * the results to the filesystem. <br/><br/>
046: *
047: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
048: * @version $Revision: 1.8 $
049: */
050: public class Encrypt {
051:
052: /**
053: * This is the entry point for the application.
054: *
055: * @param args The command-line arguments.
056: *
057: * @throws Exception If there is an error decrypting the document.
058: */
059: public static void main(String[] args) throws Exception {
060: Encrypt encrypt = new Encrypt();
061: encrypt.encrypt(args);
062: }
063:
064: private void encrypt(String[] args) throws Exception {
065: if (args.length < 1) {
066: usage();
067: } else {
068: AccessPermission ap = new AccessPermission();
069:
070: String infile = null;
071: String outfile = null;
072: String certFile = null;
073: String userPassword = "";
074: String ownerPassword = "";
075:
076: int keyLength = 48;
077:
078: PDDocument document = null;
079:
080: try {
081: for (int i = 0; i < args.length; i++) {
082: String key = args[i];
083: if (key.equals("-O")) {
084: ownerPassword = args[++i];
085: } else if (key.equals("-U")) {
086: userPassword = args[++i];
087: } else if (key.equals("-canAssemble")) {
088: ap.setCanAssembleDocument(args[++i]
089: .equalsIgnoreCase("true"));
090: } else if (key.equals("-canExtractContent")) {
091: ap.setCanExtractContent(args[++i]
092: .equalsIgnoreCase("true"));
093: } else if (key
094: .equals("-canExtractForAccessibility")) {
095: ap.setCanExtractForAccessibility(args[++i]
096: .equalsIgnoreCase("true"));
097: } else if (key.equals("-canFillInForm")) {
098: ap.setCanFillInForm(args[++i]
099: .equalsIgnoreCase("true"));
100: } else if (key.equals("-canModify")) {
101: ap.setCanModify(args[++i]
102: .equalsIgnoreCase("true"));
103: } else if (key.equals("-canModifyAnnotations")) {
104: ap.setCanModifyAnnotations(args[++i]
105: .equalsIgnoreCase("true"));
106: } else if (key.equals("-canPrint")) {
107: ap.setCanPrint(args[++i]
108: .equalsIgnoreCase("true"));
109: } else if (key.equals("-canPrintDegraded")) {
110: ap.setCanPrintDegraded(args[++i]
111: .equalsIgnoreCase("true"));
112: } else if (key.equals("-certFile")) {
113: certFile = args[++i];
114: } else if (key.equals("-keyLength")) {
115: try {
116: keyLength = Integer.parseInt(args[++i]);
117: } catch (NumberFormatException e) {
118: throw new NumberFormatException(
119: "Error: -keyLength is not an integer '"
120: + args[i] + "'");
121: }
122: } else if (infile == null) {
123: infile = key;
124: } else if (outfile == null) {
125: outfile = key;
126: } else {
127: usage();
128: }
129: }
130: if (infile == null) {
131: usage();
132: }
133: if (outfile == null) {
134: outfile = infile;
135: }
136: document = PDDocument.load(infile);
137:
138: if (!document.isEncrypted()) {
139: if (certFile != null) {
140: PublicKeyProtectionPolicy ppp = new PublicKeyProtectionPolicy();
141: PublicKeyRecipient recip = new PublicKeyRecipient();
142: recip.setPermission(ap);
143:
144: CertificateFactory cf = CertificateFactory
145: .getInstance("X.509");
146: InputStream inStream = new FileInputStream(
147: certFile);
148: X509Certificate certificate = (X509Certificate) cf
149: .generateCertificate(inStream);
150: inStream.close();
151:
152: recip.setX509(certificate);
153:
154: ppp.addRecipient(recip);
155:
156: ppp.setEncryptionKeyLength(keyLength);
157:
158: document.protect(ppp);
159: } else {
160: StandardProtectionPolicy spp = new StandardProtectionPolicy(
161: ownerPassword, userPassword, ap);
162: spp.setEncryptionKeyLength(keyLength);
163: document.protect(spp);
164: }
165: document.save(outfile);
166: } else {
167: System.err
168: .println("Error: Document is already encrypted.");
169: }
170: } finally {
171: if (document != null) {
172: document.close();
173: }
174: }
175: }
176: }
177:
178: /**
179: * This will print a usage message.
180: */
181: private static void usage() {
182: System.err
183: .println("usage: java org.pdfbox.Encrypt [options] <inputfile> [outputfile]");
184: System.err
185: .println(" -O <password> "
186: + "Set the owner password(ignored if cert is set)");
187: System.err
188: .println(" -U <password> "
189: + "Set the user password(ignored if cert is set)");
190: System.err
191: .println(" -certFile <path to cert> Path to X.509 certificate");
192: System.err
193: .println(" -canAssemble <true|false> Set the assemble permission");
194: System.err
195: .println(" -canExtractContent <true|false> Set the extraction permission");
196: System.err
197: .println(" -canExtractForAccessibility <true|false> Set the extraction permission");
198: System.err
199: .println(" -canFillInForm <true|false> Set the fill in form permission");
200: System.err
201: .println(" -canModify <true|false> Set the modify permission");
202: System.err
203: .println(" -canModifyAnnotations <true|false> Set the modify annots permission");
204: System.err
205: .println(" -canPrint <true|false> Set the print permission");
206: System.err
207: .println(" -canPrintDegraded <true|false> Set the print degraded permission");
208: System.err
209: .println(" -keyLength <length> The length of the key in bits(40)");
210: System.err
211: .println("\nNote: By default all permissions are set to true!");
212: System.exit(1);
213: }
214:
215: }
|