Example usage for org.apache.poi.poifs.crypt CryptoFunctions getCipher

List of usage examples for org.apache.poi.poifs.crypt CryptoFunctions getCipher

Introduction

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

Prototype

public static Cipher getCipher(Key key, CipherAlgorithm cipherAlgorithm, ChainingMode chain, byte[] vec,
        int cipherMode, String padding) 

Source Link

Document

Initialize a new cipher object with the given cipher properties If the given algorithm is not implemented in the JCE, it will try to load it from the bouncy castle provider.

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 www.  j a 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);
}