Search in sources :

Example 21 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project wycheproof by google.

the class CipherOutputStreamTest method testEncrypt.

@SuppressWarnings("InsecureCryptoUsage")
public void testEncrypt(Iterable<TestVector> tests) throws Exception {
    for (TestVector t : tests) {
        Cipher cipher = Cipher.getInstance(t.algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, t.key, t.params);
        cipher.updateAAD(t.aad);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        cos.write(t.pt);
        cos.close();
        assertEquals(TestUtil.bytesToHex(t.ct), TestUtil.bytesToHex(os.toByteArray()));
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project wycheproof by google.

the class CipherOutputStreamTest method testDecrypt.

@SuppressWarnings("InsecureCryptoUsage")
public void testDecrypt(Iterable<TestVector> tests) throws Exception {
    for (TestVector t : tests) {
        Cipher cipher = Cipher.getInstance(t.algorithm);
        cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
        cipher.updateAAD(t.aad);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        cos.write(t.ct);
        cos.close();
        assertEquals(TestUtil.bytesToHex(t.pt), TestUtil.bytesToHex(os.toByteArray()));
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 23 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project wycheproof by google.

the class CipherOutputStreamTest method testCorruptDecrypt.

@SuppressWarnings("InsecureCryptoUsage")
public void testCorruptDecrypt(Iterable<TestVector> tests) throws Exception {
    for (TestVector t : tests) {
        Cipher cipher = Cipher.getInstance(t.algorithm);
        cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
        cipher.updateAAD(t.aad);
        byte[] ct = Arrays.copyOf(t.ct, t.ct.length);
        ct[ct.length - 1] ^= (byte) 1;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        cos.write(ct);
        try {
            // cos.close() should call cipher.doFinal().
            cos.close();
            byte[] decrypted = os.toByteArray();
            // between beheviour considered acceptable by Oracle and more serious flaws.
            if (decrypted.length > 0) {
                fail("this should fail; decrypted:" + TestUtil.bytesToHex(decrypted) + " pt: " + TestUtil.bytesToHex(t.pt));
            }
        } catch (IOException ex) {
        // expected
        }
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 24 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project RxCache by VictorAlbertos.

the class BuiltInEncryptor method decrypt.

@Override
public void decrypt(String key, File encryptedFile, File decryptedFile) {
    initCiphers(key);
    try {
        CipherOutputStream cos = new CipherOutputStream(new FileOutputStream(decryptedFile), decryptCipher);
        write(new FileInputStream(encryptedFile), cos);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 25 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