Method from org.apache.pdfbox.pdmodel.encryption.SecurityHandler Detail: |
abstract public void decryptDocument(PDDocument doc,
DecryptionMaterial mat) throws CryptographyException, IOException
Prepare the document for decryption. |
public void decryptStream(COSStream stream,
long objNum,
long genNum) throws CryptographyException, IOException {
decryptDictionary( stream, objNum, genNum );
InputStream encryptedStream = stream.getFilteredStream();
encryptData( objNum,
genNum,
encryptedStream,
stream.createFilteredStream() );
}
This will decrypt a stream. |
public void decryptString(COSString string,
long objNum,
long genNum) throws CryptographyException, IOException {
ByteArrayInputStream data = new ByteArrayInputStream( string.getBytes() );
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
encryptData( objNum, genNum, data, buffer );
string.reset();
string.append( buffer.toByteArray() );
}
This will decrypt a string. |
public void encryptData(long objectNumber,
long genNumber,
InputStream data,
OutputStream output) throws CryptographyException, IOException {
byte[] newKey = new byte[ encryptionKey.length + 5 ];
System.arraycopy( encryptionKey, 0, newKey, 0, encryptionKey.length );
//PDF 1.4 reference pg 73
//step 1
//we have the reference
//step 2
newKey[newKey.length -5] = (byte)(objectNumber & 0xff);
newKey[newKey.length -4] = (byte)((objectNumber > > 8) & 0xff);
newKey[newKey.length -3] = (byte)((objectNumber > > 16) & 0xff);
newKey[newKey.length -2] = (byte)(genNumber & 0xff);
newKey[newKey.length -1] = (byte)((genNumber > > 8) & 0xff);
//step 3
byte[] digestedKey = null;
try
{
MessageDigest md = MessageDigest.getInstance( "MD5" );
digestedKey = md.digest( newKey );
}
catch( NoSuchAlgorithmException e )
{
throw new CryptographyException( e );
}
//step 4
int length = Math.min( newKey.length, 16 );
byte[] finalKey = new byte[ length ];
System.arraycopy( digestedKey, 0, finalKey, 0, length );
rc4.setKey( finalKey );
rc4.write( data, output );
output.flush();
}
|
public AccessPermission getCurrentAccessPermission() {
return currentAccessPermission;
}
Returns the access permissions that were computed during document decryption.
The returned object is in read only mode. |
public int getKeyLength() {
return keyLength;
}
Getter of the property keyLength. |
abstract public void prepareDocumentForEncryption(PDDocument doc) throws CryptographyException, IOException
Prepare the document for encryption. |
protected void proceedDecryption() throws IOException, CryptographyException {
COSDictionary trailer = document.getDocument().getTrailer();
COSArray fields = (COSArray)trailer.getObjectFromPath( "Root/AcroForm/Fields" );
//We need to collect all the signature dictionaries, for some
//reason the 'Contents' entry of signatures is not really encrypted
if( fields != null )
{
for( int i=0; i< fields.size(); i++ )
{
COSDictionary field = (COSDictionary)fields.getObject( i );
addDictionaryAndSubDictionary( potentialSignatures, field );
}
}
List allObjects = document.getDocument().getObjects();
Iterator objectIter = allObjects.iterator();
while( objectIter.hasNext() )
{
decryptObject( (COSObject)objectIter.next() );
}
document.setEncryptionDictionary( null );
}
This method must be called by an implementation of this class to really proceed
to decryption. |
public void setKeyLength(int keyLen) {
this.keyLength = keyLen;
}
Setter of the property keyLength. |