Search in sources :

Example 11 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project poi by apache.

the class AesZipFileZipEntrySource method copyToFile.

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;
    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();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) CipherOutputStream(javax.crypto.CipherOutputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Cipher(javax.crypto.Cipher) FilterOutputStream(java.io.FilterOutputStream)

Example 12 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project poi by apache.

the class SheetDataWriterWithDecorator method decorateOutputStream.

@Override
protected OutputStream decorateOutputStream(FileOutputStream fos) {
    init();
    Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding");
    return new CipherOutputStream(fos, ciEnc);
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher)

Example 13 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project Conversations by siacs.

the class AbstractConnectionManager method createOutputStream.

private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
    FileOutputStream os;
    try {
        os = new FileOutputStream(file, append);
        if (file.getKey() == null) {
            return os;
        }
    } catch (FileNotFoundException e) {
        return null;
    }
    try {
        if (gcm) {
            AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
            cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
            return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
        } else {
            IvParameterSpec ips = new IvParameterSpec(file.getIv());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
            Log.d(Config.LOGTAG, "opening encrypted output stream");
            return new CipherOutputStream(os, cipher);
        }
    } catch (InvalidKeyException e) {
        return null;
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (NoSuchPaddingException e) {
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        return null;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) CipherOutputStream(javax.crypto.CipherOutputStream) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) FileNotFoundException(java.io.FileNotFoundException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) SecretKeySpec(javax.crypto.spec.SecretKeySpec) FileOutputStream(java.io.FileOutputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher)

Example 14 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project jdk8u_jdk by JetBrains.

the class CICODESFuncTest method runTest.

private static void runTest(Provider p, String algo, String mo, String pad, ReadModel whichRead) throws GeneralSecurityException, IOException {
    // Do initialization
    byte[] plainText = TestUtilities.generateBytes(TEXT_LENGTH);
    byte[] iv = TestUtilities.generateBytes(IV_LENGTH);
    AlgorithmParameterSpec aps = new IvParameterSpec(iv);
    try {
        KeyGenerator kg = KeyGenerator.getInstance(algo, p);
        out.println(algo + "/" + mo + "/" + pad + "/" + whichRead);
        SecretKey key = kg.generateKey();
        Cipher ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);
        if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) {
            throw new RuntimeException("NoSuchAlgorithmException not throw when mode" + " is CFB72 or OFB20");
        }
        Cipher ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p);
        if ("ECB".equalsIgnoreCase(mo)) {
            ci1.init(Cipher.ENCRYPT_MODE, key);
            ci2.init(Cipher.DECRYPT_MODE, key);
        } else {
            ci1.init(Cipher.ENCRYPT_MODE, key, aps);
            ci2.init(Cipher.DECRYPT_MODE, key, aps);
        }
        ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
        try (CipherInputStream cInput = new CipherInputStream(new ByteArrayInputStream(plainText), ci1);
            CipherOutputStream ciOutput = new CipherOutputStream(baOutput, ci2)) {
            // Read from the input and write to the output using 2 types
            // of buffering : byte[] and int
            whichRead.read(cInput, ciOutput, ci1, plainText.length);
        }
        // Verify input and output are same.
        if (!Arrays.equals(plainText, baOutput.toByteArray())) {
            throw new RuntimeException("Test failed due to compare fail ");
        }
    } catch (NoSuchAlgorithmException nsaEx) {
        if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) {
            out.println("NoSuchAlgorithmException is expected for CFB72 and OFB20");
        } else {
            throw new RuntimeException("Unexpected exception testing: " + algo + "/" + mo + "/" + pad + "/" + whichRead, nsaEx);
        }
    }
}
Also used : SecretKey(javax.crypto.SecretKey) CipherOutputStream(javax.crypto.CipherOutputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 15 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project syncany by syncany.

the class AesGcmWithBcInputStreamTest method encryptWithAesGcm.

private static byte[] encryptWithAesGcm(byte[] plaintext, byte[] randomKeyBytes, byte[] randomIvBytes) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException {
    SecretKey randomKey = new SecretKeySpec(randomKeyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
    cipher.init(Cipher.ENCRYPT_MODE, randomKey, new IvParameterSpec(randomIvBytes));
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    CipherOutputStream cipherOutputStream = new CipherOutputStream(byteArrayOutputStream, cipher);
    cipherOutputStream.write(plaintext);
    cipherOutputStream.close();
    return byteArrayOutputStream.toByteArray();
}
Also used : SecretKey(javax.crypto.SecretKey) CipherOutputStream(javax.crypto.CipherOutputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

CipherOutputStream (javax.crypto.CipherOutputStream)39 Cipher (javax.crypto.Cipher)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)13 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 CipherInputStream (javax.crypto.CipherInputStream)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)7 FileOutputStream (java.io.FileOutputStream)6 SecretKey (javax.crypto.SecretKey)6 OutputStream (java.io.OutputStream)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NullCipher (javax.crypto.NullCipher)5 InvalidKeyException (java.security.InvalidKeyException)4 KeyGenerator (javax.crypto.KeyGenerator)4 BufferedOutputStream (java.io.BufferedOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)3 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)3