Example usage for org.apache.poi.poifs.crypt ChainingMode cbc

List of usage examples for org.apache.poi.poifs.crypt ChainingMode cbc

Introduction

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

Prototype

ChainingMode cbc

To view the source code for org.apache.poi.poifs.crypt ChainingMode cbc.

Click Source Link

Usage

From source file:com.github.poi.AesZipFileZipEntrySource.java

License:Apache License

private static void copyToFile(InputStream is, File tmpFile, CipherAlgorithm cipherAlgorithm, byte keyBytes[],
        byte ivBytes[]) throws IOException, GeneralSecurityException {
    SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
    Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes,
            Cipher.ENCRYPT_MODE, "PKCS5Padding");

    ZipInputStream zis = new ZipInputStream(is);
    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipOutputStream zos = new ZipOutputStream(fos);

    ZipEntry ze;//from   w ww.ja  v  a 2 s .  c o  m
    while ((ze = zis.getNextEntry()) != null) {
        // the cipher output stream pads the data, therefore we can't reuse the ZipEntry with set sizes
        // as those will be validated upon close()
        ZipEntry zeNew = new ZipEntry(ze.getName());
        zeNew.setComment(ze.getComment());
        zeNew.setExtra(ze.getExtra());
        zeNew.setTime(ze.getTime());
        // zeNew.setMethod(ze.getMethod());
        zos.putNextEntry(zeNew);
        FilterOutputStream fos2 = new FilterOutputStream(zos) {
            // don't close underlying ZipOutputStream
            @Override
            public void close() {
            }
        };
        CipherOutputStream cos = new CipherOutputStream(fos2, ciEnc);
        IOUtils.copy(zis, cos);
        cos.close();
        fos2.close();
        zos.closeEntry();
        zis.closeEntry();
    }
    zos.close();
    fos.close();
    zis.close();
}

From source file:com.github.poi.AesZipFileZipEntrySource.java

License:Apache License

private static AesZipFileZipEntrySource fileToSource(File tmpFile, CipherAlgorithm cipherAlgorithm,
        byte keyBytes[], byte ivBytes[]) throws ZipException, IOException {
    SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
    Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes,
            Cipher.DECRYPT_MODE, "PKCS5Padding");
    return new AesZipFileZipEntrySource(tmpFile, ciDec);
}