Example usage for org.apache.poi.poifs.crypt EncryptionInfo getEncryptor

List of usage examples for org.apache.poi.poifs.crypt EncryptionInfo getEncryptor

Introduction

In this page you can find the example usage for org.apache.poi.poifs.crypt EncryptionInfo getEncryptor.

Prototype

public Encryptor getEncryptor() 

Source Link

Usage

From source file:de.jlo.talendcomp.excel.SpreadsheetFile.java

License:Apache License

private static void encryptFile(String inFilePath, String outFilePath, String password) throws Exception {
    if (password == null || password.trim().isEmpty()) {
        throw new Exception("Password cannot be null or empty!");
    }//  ww  w.  ja va 2s  .co m
    if (inFilePath == null || inFilePath.trim().isEmpty()) {
        throw new Exception("Input file cannot be null or empty!");
    }
    File inFile = new File(inFilePath);
    if (outFilePath == null || outFilePath.trim().isEmpty()) {
        throw new Exception("Output file cannot be null or empty!");
    }
    File outFile = new File(outFilePath);
    if (inFile.exists() == false) {
        throw new Exception("Excel file to encrypt: " + inFile.getAbsolutePath() + " does not exists!");
    }
    ensureDirExists(outFile);
    POIFSFileSystem fs = new POIFSFileSystem();
    EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
    Encryptor enc = info.getEncryptor();
    enc.confirmPassword(password);
    OPCPackage opc = OPCPackage.open(inFile, PackageAccess.READ_WRITE);
    OutputStream os = enc.getDataStream(fs);
    opc.save(os);
    opc.close();
    FileOutputStream fos = null;
    Exception ex = null;
    try {
        fos = new FileOutputStream(outFile);
        fs.writeFilesystem(fos);
    } catch (Exception e) {
        ex = e;
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e1) {
                // ignore
            }
        }
    }
    if (ex != null) {
        throw ex;
    }
}

From source file:uk.ac.ucl.cs.cmic.giftcloud.uploader.ExcelWriter.java

License:Open Source License

@Override
protected void saveFile(final File file) throws IOException {
    if (spreadsheetPassword.isPresent() && StringUtils.isNotBlank(new String(spreadsheetPassword.get()))) {
        try {// www . j a  v  a  2  s . com
            // Write the unencrypted spreadsheet to an in-memory stream
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            workbook.write(baos);
            baos.close();

            // Create objects for encryption
            POIFSFileSystem fs = new POIFSFileSystem();
            EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
            Encryptor enc = info.getEncryptor();

            // Set the password
            enc.confirmPassword(new String(spreadsheetPassword.get()));

            // Open the in-memory spreadsheet
            InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
            OPCPackage opc = OPCPackage.open(inputStream);
            OutputStream os = enc.getDataStream(fs);
            opc.save(os);
            opc.close();
            inputStream.close();

            // Write the encrypted Excel spreadsheet
            FileOutputStream fos = new FileOutputStream(file);
            fs.writeFilesystem(fos);
            fos.close();
        } catch (InvalidFormatException e) {
            throw new IOException(
                    "Unable to save the patient list file due to the following InvalidFormatException when reading the excel file:"
                            + e.getLocalizedMessage(),
                    e);
        } catch (GeneralSecurityException e) {
            throw new IOException(
                    "Unable to save the patient list file due to the following GeneralSecurityException when reading the excel file:"
                            + e.getLocalizedMessage(),
                    e);
        }
    } else {
        // Write the unencrypted spreadsheet to an in-memory stream
        final FileOutputStream fos = new FileOutputStream(file);
        workbook.write(fos);
        fos.close();
    }
}