1 31 package org.pdfbox; 32 33 import java.io.FileInputStream ; 34 import java.io.IOException ; 35 import java.security.KeyStore ; 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 52 public class Decrypt 53 { 54 private static final String ALIAS = "-alias"; 55 private static final String PASSWORD = "-password"; 56 private static final String KEYSTORE = "-keyStore"; 57 58 59 66 public static void main( String [] args ) throws Exception 67 { 68 Decrypt decrypt = new Decrypt(); 69 decrypt.decrypt( args ); 70 } 71 72 private void decrypt( String [] args ) throws Exception 73 { 74 if( args.length < 2 || args.length > 3 ) 75 { 76 usage(); 77 } 78 else 79 { 80 String password = null; 81 String infile = null; 82 String outfile = null; 83 String alias = null; 84 String 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 ks = KeyStore.getInstance("PKCS12"); 153 ks.load(new FileInputStream (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 ( 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 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 |