Example usage for com.lowagie.text.pdf PdfWriter ENCRYPTION_AES_128

List of usage examples for com.lowagie.text.pdf PdfWriter ENCRYPTION_AES_128

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter ENCRYPTION_AES_128.

Prototype

int ENCRYPTION_AES_128

To view the source code for com.lowagie.text.pdf PdfWriter ENCRYPTION_AES_128.

Click Source Link

Document

Type of encryption

Usage

From source file:org.pdfsam.console.business.pdf.handlers.EncryptCmdExecutor.java

License:Open Source License

public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException {

    if ((parsedCommand != null) && (parsedCommand instanceof EncryptParsedCommand)) {

        EncryptParsedCommand inputCommand = (EncryptParsedCommand) parsedCommand;
        setPercentageOfWorkDone(0);// w  ww .j  a v a  2  s .  c om
        int encType = PdfWriter.STANDARD_ENCRYPTION_40;
        PrefixParser prefixParser;
        try {
            PdfFile[] fileList = arraysConcat(inputCommand.getInputFileList(),
                    getPdfFiles(inputCommand.getInputDirectory()));
            // check if empty
            if (fileList == null || !(fileList.length > 0)) {
                throw new EncryptException(EncryptException.CMD_NO_INPUT_FILE);
            }
            for (int i = 0; i < fileList.length; i++) {
                try {
                    // set the encryption type
                    if (EncryptParsedCommand.E_AES_128.equals(inputCommand.getEncryptionType())) {
                        encType = PdfWriter.ENCRYPTION_AES_128;
                    } else if (EncryptParsedCommand.E_RC4_128.equals(inputCommand.getEncryptionType())) {
                        encType = PdfWriter.STANDARD_ENCRYPTION_128;
                    }

                    prefixParser = new PrefixParser(inputCommand.getOutputFilesPrefix(),
                            fileList[i].getFile().getName());
                    File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile());
                    pdfReader = PdfUtility.readerFor(fileList[i]);
                    pdfReader.removeUnusedObjects();
                    pdfReader.consolidateNamedDestinations();

                    // version
                    LOG.debug("Creating a new document.");
                    Character pdfVersion = inputCommand.getOutputPdfVersion();
                    if (pdfVersion != null) {
                        pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(tmpFile),
                                inputCommand.getOutputPdfVersion().charValue());
                    } else {
                        pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(tmpFile),
                                pdfReader.getPdfVersion());
                    }

                    HashMap meta = pdfReader.getInfo();
                    meta.put("Creator", ConsoleServicesFacade.CREATOR);

                    setCompressionSettingOnStamper(inputCommand, pdfStamper);

                    pdfStamper.setMoreInfo(meta);
                    pdfStamper.setEncryption(encType, inputCommand.getUserPwd(), inputCommand.getOwnerPwd(),
                            inputCommand.getPermissions());
                    pdfStamper.close();
                    pdfReader.close();
                    File outFile = new File(inputCommand.getOutputFile(), prefixParser.generateFileName());
                    FileUtility.renameTemporaryFile(tmpFile, outFile, inputCommand.isOverwrite());
                    LOG.debug("Encrypted file " + outFile.getCanonicalPath() + " created.");
                    setPercentageOfWorkDone(((i + 1) * WorkDoneDataModel.MAX_PERGENTAGE) / fileList.length);
                } catch (Exception e) {
                    LOG.error("Error encrypting file " + fileList[i].getFile().getName(), e);
                }
            }
            LOG.info("Pdf files encrypted in " + inputCommand.getOutputFile().getAbsolutePath() + ".");
            LOG.info("Permissions: " + PdfEncryptor.getPermissionsVerbose(inputCommand.getPermissions()) + ".");
        } catch (Exception e) {
            throw new EncryptException(e);
        } finally {
            setWorkCompleted();
        }
    } else {
        throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND);
    }
}

From source file:org.pdfsam.guiclient.commons.business.loaders.callable.AddPdfDocument.java

License:Open Source License

/**
* 
* @param fileToAdd file to add/*from ww w .jav  a  2  s. c om*/
* @param password password to open the file
* @return the item to add to the table
*/
PdfSelectionTableItem getPdfSelectionTableItem(File fileToAdd, String password, String pageSelection) {
    PdfSelectionTableItem tableItem = null;
    PdfReader pdfReader = null;
    if (fileToAdd != null) {
        tableItem = new PdfSelectionTableItem();
        tableItem.setInputFile(fileToAdd);
        tableItem.setPassword(password);
        tableItem.setPageSelection(pageSelection);
        try {
            //fix 04/11/08 for memory usage
            pdfReader = new PdfReader(new RandomAccessFileOrArray(fileToAdd.getAbsolutePath()),
                    (password != null) ? password.getBytes() : null);
            tableItem.setEncrypted(pdfReader.isEncrypted());
            tableItem.setFullPermission(pdfReader.isOpenedWithFullPermissions());
            if (tableItem.isEncrypted()) {
                tableItem.setPermissions(getPermissionsVerbose(pdfReader.getPermissions()));
                int cMode = pdfReader.getCryptoMode();
                switch (cMode) {
                case PdfWriter.STANDARD_ENCRYPTION_40:
                    tableItem.setEncryptionAlgorithm(EncryptionUtility.RC4_40);
                    break;
                case PdfWriter.STANDARD_ENCRYPTION_128:
                    tableItem.setEncryptionAlgorithm(EncryptionUtility.RC4_128);
                    break;
                case PdfWriter.ENCRYPTION_AES_128:
                    tableItem.setEncryptionAlgorithm(EncryptionUtility.AES_128);
                    break;
                default:
                    break;
                }
            }
            tableItem.setPagesNumber(Integer.toString(pdfReader.getNumberOfPages()));
            tableItem.setFileSize(fileToAdd.length());
            tableItem.setPdfVersion(pdfReader.getPdfVersion());
            tableItem.setSyntaxErrors(pdfReader.isRebuilt());
            initTableItemDocumentData(pdfReader, tableItem);
        } catch (Exception e) {
            tableItem.setLoadedWithErrors(true);
            LOG.error(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),
                    "Error loading ") + fileToAdd.getAbsolutePath() + " :", e);
        } finally {
            if (pdfReader != null) {
                pdfReader.close();
                pdfReader = null;
            }
        }
    }
    return tableItem;
}

From source file:org.sejda.impl.itext.util.EncryptionUtils.java

License:Apache License

/**
 * Mapping between Sejda and iText encryption algorithms
 * //  ww w  . j a  v  a 2  s.c  o  m
 * @param encryption
 * @return the iText encryption constant
 */
public static int getEncryptionAlgorithm(PdfEncryption encryption) {
    switch (encryption) {
    case AES_ENC_128:
        return PdfWriter.ENCRYPTION_AES_128;
    case STANDARD_ENC_128:
        return PdfWriter.STANDARD_ENCRYPTION_128;
    default:
        return PdfWriter.STANDARD_ENCRYPTION_40;
    }
}

From source file:questions.encryption.HelloWorldFullyEncrypted.java

public static void main(String[] args) {
    // step 1//ww w . j a  v a 2s. c o  m
    Document document = new Document();
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setEncryption("hello".getBytes(), "world".getBytes(), 0, PdfWriter.ENCRYPTION_AES_128);
        writer.createXmpMetadata();
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f),
                "This is some text", "some text".getBytes(), null, "some.txt"));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}

From source file:questions.encryption.HelloWorldMetadataNotEncrypted.java

public static void main(String[] args) {
    // step 1//from ww  w  .  j av  a 2 s .  co  m
    Document document = new Document();
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setEncryption("hello".getBytes(), "world".getBytes(), 0,
                PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
        writer.createXmpMetadata();
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f),
                "This is some text", "some text".getBytes(), null, "some.txt"));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}