Search in sources :

Example 1 with StandardDecryptionMaterial

use of com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial in project PdfBox-Android by TomRoush.

the class COSParser method prepareDecryption.

/**
 * Prepare for decryption.
 *
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException if something went wrong
 */
private void prepareDecryption() throws IOException {
    if (encryption != null) {
        return;
    }
    COSBase trailerEncryptItem = document.getTrailer().getItem(COSName.ENCRYPT);
    if (trailerEncryptItem == null || trailerEncryptItem instanceof COSNull) {
        return;
    }
    if (trailerEncryptItem instanceof COSObject) {
        COSObject trailerEncryptObj = (COSObject) trailerEncryptItem;
        parseDictionaryRecursive(trailerEncryptObj);
    }
    try {
        encryption = new PDEncryption(document.getEncryptionDictionary());
        DecryptionMaterial decryptionMaterial;
        if (keyStoreInputStream != null) {
            KeyStore ks = KeyStore.getInstance("PKCS12");
            ks.load(keyStoreInputStream, password.toCharArray());
            decryptionMaterial = new PublicKeyDecryptionMaterial(ks, keyAlias, password);
        } else {
            decryptionMaterial = new StandardDecryptionMaterial(password);
        }
        securityHandler = encryption.getSecurityHandler();
        securityHandler.prepareForDecryption(encryption, document.getDocumentID(), decryptionMaterial);
        accessPermission = securityHandler.getCurrentAccessPermission();
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Error (" + e.getClass().getSimpleName() + ") while creating security handler for decryption", e);
    } finally {
        if (keyStoreInputStream != null) {
            IOUtils.closeQuietly(keyStoreInputStream);
        }
    }
}
Also used : DecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.DecryptionMaterial) PublicKeyDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial) StandardDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSNull(com.tom_roush.pdfbox.cos.COSNull) PublicKeyDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial) StandardDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) PDEncryption(com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption) IOException(java.io.IOException) KeyStore(java.security.KeyStore) InvalidPasswordException(com.tom_roush.pdfbox.pdmodel.encryption.InvalidPasswordException) IOException(java.io.IOException)

Aggregations

COSBase (com.tom_roush.pdfbox.cos.COSBase)1 COSNull (com.tom_roush.pdfbox.cos.COSNull)1 COSObject (com.tom_roush.pdfbox.cos.COSObject)1 DecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.DecryptionMaterial)1 InvalidPasswordException (com.tom_roush.pdfbox.pdmodel.encryption.InvalidPasswordException)1 PDEncryption (com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption)1 PublicKeyDecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial)1 StandardDecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial)1 IOException (java.io.IOException)1 KeyStore (java.security.KeyStore)1