📄 documentencryption.java
字号:
if( isUserPassword )
{
encryptionKey =
encryption.computeEncryptedKey(
password.getBytes(), o,
permissions, id.getBytes(), revision, length );
}
else if( isOwnerPassword )
{
byte[] computedUserPassword =
encryption.getUserPassword(
password.getBytes(),
o,
revision,
length );
encryptionKey =
encryption.computeEncryptedKey(
computedUserPassword, o,
permissions, id.getBytes(), revision, length );
}
else
{
throw new InvalidPasswordException( "Error: The supplied password does not match " +
"either the owner or user password in the document." );
}
COSDictionary trailer = document.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.getObjects();
Iterator objectIter = allObjects.iterator();
while( objectIter.hasNext() )
{
decryptObject( (COSObject)objectIter.next() );
}
document.setEncryptionDictionary( null );
}
private void addDictionaryAndSubDictionary( Set set, COSDictionary dic )
{
set.add( dic );
COSArray kids = (COSArray)dic.getDictionaryObject( "Kids" );
for( int i=0; kids != null && i<kids.size(); i++ )
{
addDictionaryAndSubDictionary( set, (COSDictionary)kids.getObject( i ) );
}
COSBase value = dic.getDictionaryObject( "V" );
if( value instanceof COSDictionary )
{
addDictionaryAndSubDictionary( set, (COSDictionary)value );
}
}
/**
* This will decrypt an object in the document.
*
* @param object The object to decrypt.
*
* @throws CryptographyException If there is an error decrypting the stream.
* @throws IOException If there is an error getting the stream data.
*/
private void decryptObject( COSObject object )
throws CryptographyException, IOException
{
long objNum = object.getObjectNumber().intValue();
long genNum = object.getGenerationNumber().intValue();
COSBase base = object.getObject();
decrypt( base, objNum, genNum );
}
/**
* This will dispatch to the correct method.
*
* @param obj The object to decrypt.
* @param objNum The object number.
* @param genNum The object generation Number.
*
* @throws CryptographyException If there is an error decrypting the stream.
* @throws IOException If there is an error getting the stream data.
*/
public void decrypt( Object obj, long objNum, long genNum )
throws CryptographyException, IOException
{
if( !objects.contains( obj ) )
{
objects.add( obj );
if( obj instanceof COSString )
{
decryptString( (COSString)obj, objNum, genNum );
}
else if( obj instanceof COSStream )
{
decryptStream( (COSStream)obj, objNum, genNum );
}
else if( obj instanceof COSDictionary )
{
decryptDictionary( (COSDictionary)obj, objNum, genNum );
}
else if( obj instanceof COSArray )
{
decryptArray( (COSArray)obj, objNum, genNum );
}
}
}
/**
* This will decrypt a stream.
*
* @param stream The stream to decrypt.
* @param objNum The object number.
* @param genNum The object generation number.
*
* @throws CryptographyException If there is an error getting the stream.
* @throws IOException If there is an error getting the stream data.
*/
private void decryptStream( COSStream stream, long objNum, long genNum )
throws CryptographyException, IOException
{
decryptDictionary( stream, objNum, genNum );
InputStream encryptedStream = stream.getFilteredStream();
encryption.encryptData( objNum,
genNum,
encryptionKey,
encryptedStream,
stream.createFilteredStream() );
}
/**
* This will decrypt a dictionary.
*
* @param dictionary The dictionary to decrypt.
* @param objNum The object number.
* @param genNum The object generation number.
*
* @throws CryptographyException If there is an error decrypting the document.
* @throws IOException If there is an error creating a new string.
*/
private void decryptDictionary( COSDictionary dictionary, long objNum, long genNum )
throws CryptographyException, IOException
{
Iterator keys = dictionary.keyList().iterator();
while( keys.hasNext() )
{
COSName key = (COSName)keys.next();
Object value = dictionary.getItem( key );
//if we are a signature dictionary and contain a Contents entry then
//we don't decrypt it.
if( !(key.getName().equals( "Contents" ) &&
value instanceof COSString &&
potentialSignatures.contains( dictionary )))
{
decrypt( value, objNum, genNum );
}
}
}
/**
* This will decrypt a string.
*
* @param string the string to decrypt.
* @param objNum The object number.
* @param genNum The object generation number.
*
* @throws CryptographyException If an error occurs during decryption.
* @throws IOException If an error occurs writing the new string.
*/
private void decryptString( COSString string, long objNum, long genNum )
throws CryptographyException, IOException
{
ByteArrayInputStream data = new ByteArrayInputStream( string.getBytes() );
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
encryption.encryptData( objNum,
genNum,
encryptionKey,
data,
buffer );
string.reset();
string.append( buffer.toByteArray() );
}
/**
* This will decrypt an array.
*
* @param array The array to decrypt.
* @param objNum The object number.
* @param genNum The object generation number.
*
* @throws CryptographyException If an error occurs during decryption.
* @throws IOException If there is an error accessing the data.
*/
private void decryptArray( COSArray array, long objNum, long genNum )
throws CryptographyException, IOException
{
for( int i=0; i<array.size(); i++ )
{
decrypt( array.get( i ), objNum, genNum );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -