1 31 32 package org.pdfbox.pdmodel.encryption; 33 34 import java.io.IOException ; 35 36 import org.pdfbox.cos.COSArray; 37 import org.pdfbox.cos.COSDictionary; 38 import org.pdfbox.cos.COSName; 39 import org.pdfbox.cos.COSString; 40 41 54 public class PDEncryptionDictionary 55 { 56 59 public static final int VERSION0_UNDOCUMENTED_UNSUPPORTED = 0; 60 63 public static final int VERSION1_40_BIT_ALGORITHM = 1; 64 67 public static final int VERSION2_VARIABLE_LENGTH_ALGORITHM = 2; 68 71 public static final int VERSION3_UNPUBLISHED_ALGORITHM = 3; 72 75 public static final int VERSION4_SECURITY_HANDLER = 4; 76 77 80 public static final String DEFAULT_NAME = "Standard"; 81 82 85 public static final int DEFAULT_LENGTH = 40; 86 87 90 public static final int DEFAULT_VERSION = VERSION0_UNDOCUMENTED_UNSUPPORTED; 91 92 95 protected COSDictionary encryptionDictionary = null; 96 97 100 public PDEncryptionDictionary() 101 { 102 encryptionDictionary = new COSDictionary(); 103 } 104 105 109 public PDEncryptionDictionary(COSDictionary d) 110 { 111 encryptionDictionary = d; 112 } 113 114 119 public COSDictionary getCOSDictionary() 120 { 121 return encryptionDictionary; 122 } 123 124 129 public void setFilter(String filter) 130 { 131 encryptionDictionary.setItem( COSName.FILTER, COSName.getPDFName( filter ) ); 132 } 133 134 139 public String getFilter() 140 { 141 return encryptionDictionary.getNameAsString( COSName.FILTER ); 142 } 143 144 149 public void setSubFilter(String subfilter) 150 { 151 encryptionDictionary.setName( "SubFilter", subfilter ); 152 } 153 154 162 public void setVersion(int version) 163 { 164 encryptionDictionary.setInt( "V", version ); 165 } 166 167 173 public int getVersion() 174 { 175 return encryptionDictionary.getInt( "V", 0 ); 176 } 177 178 183 public void setLength(int length) 184 { 185 encryptionDictionary.setInt("Length", length); 186 } 187 188 194 public int getLength() 195 { 196 return encryptionDictionary.getInt( "Length", 40 ); 197 } 198 199 208 public void setRevision(int revision) 209 { 210 encryptionDictionary.setInt( "R", revision ); 211 } 212 213 219 public int getRevision() 220 { 221 return encryptionDictionary.getInt( "R", DEFAULT_VERSION ); 222 } 223 224 231 public void setOwnerKey(byte[] o) throws IOException 232 { 233 COSString owner = new COSString(); 234 owner.append( o ); 235 encryptionDictionary.setItem( COSName.getPDFName( "O" ), owner ); 236 } 237 238 245 public byte[] getOwnerKey() throws IOException 246 { 247 byte[] o = null; 248 COSString owner = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "O" ) ); 249 if( owner != null ) 250 { 251 o = owner.getBytes(); 252 } 253 return o; 254 } 255 256 263 public void setUserKey(byte[] u) throws IOException 264 { 265 COSString user = new COSString(); 266 user.append( u ); 267 encryptionDictionary.setItem( COSName.getPDFName( "U" ), user ); 268 } 269 270 277 public byte[] getUserKey() throws IOException 278 { 279 byte[] u = null; 280 COSString user = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "U" ) ); 281 if( user != null ) 282 { 283 u = user.getBytes(); 284 } 285 return u; 286 } 287 288 293 public void setPermissions(int permissions) 294 { 295 encryptionDictionary.setInt( "P", permissions ); 296 } 297 298 303 public int getPermissions() 304 { 305 return encryptionDictionary.getInt( "P", 0 ); 306 } 307 308 314 public void setRecipients(byte[][] recipients) throws IOException 315 { 316 COSArray array = new COSArray(); 317 for(int i=0; i<recipients.length; i++) 318 { 319 COSString recip = new COSString(); 320 recip.append(recipients[i]); 321 recip.setForceLiteralForm(true); 322 array.add(recip); 323 } 324 encryptionDictionary.setItem(COSName.getPDFName("Recipients"), array); 325 } 326 327 332 public int getRecipientsLength() 333 { 334 COSArray array = (COSArray)encryptionDictionary.getItem(COSName.getPDFName("Recipients")); 335 return array.size(); 336 } 337 338 345 public COSString getRecipientStringAt(int i) 346 { 347 COSArray array = (COSArray)encryptionDictionary.getItem(COSName.getPDFName("Recipients")); 348 return (COSString)array.get(i); 349 } 350 } | Popular Tags |