1 31 package org.pdfbox; 32 33 import java.io.FileInputStream ; 34 import java.io.InputStream ; 35 import java.security.cert.CertificateFactory ; 36 import java.security.cert.X509Certificate ; 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 51 public class Encrypt 52 { 53 54 61 public static void main( String [] args ) throws Exception 62 { 63 Encrypt encrypt = new Encrypt(); 64 encrypt.encrypt( args ); 65 } 66 67 private void encrypt( String [] args ) throws Exception 68 { 69 if( args.length < 1 ) 70 { 71 usage(); 72 } 73 else 74 { 75 AccessPermission ap = new AccessPermission(); 76 77 String infile = null; 78 String outfile = null; 79 String certFile = null; 80 String userPassword = ""; 81 String 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 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 e ) 143 { 144 throw new NumberFormatException ( 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 cf = CertificateFactory.getInstance("X.509"); 181 InputStream inStream = new FileInputStream (certFile); 182 X509Certificate certificate = (X509Certificate )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 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 |