Search in sources :

Example 1 with CipherOutputStream

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

the class CryptoDataFormat method marshal.

public void marshal(Exchange exchange, Object graph, OutputStream outputStream) throws Exception {
    byte[] iv = getInitializationVector(exchange);
    Key key = getKey(exchange);
    InputStream plaintextStream = ExchangeHelper.convertToMandatoryType(exchange, InputStream.class, graph);
    HMACAccumulator hmac = getMessageAuthenticationCode(key);
    if (plaintextStream != null) {
        inlineInitVector(outputStream, iv);
        byte[] buffer = new byte[bufferSize];
        int read;
        CipherOutputStream cipherStream = null;
        try {
            cipherStream = new CipherOutputStream(outputStream, initializeCipher(ENCRYPT_MODE, key, iv));
            while ((read = plaintextStream.read(buffer)) > 0) {
                cipherStream.write(buffer, 0, read);
                cipherStream.flush();
                hmac.encryptUpdate(buffer, read);
            }
            // only write if there is data to write (IBM JDK throws exception if no data)
            byte[] mac = hmac.getCalculatedMac();
            if (mac != null && mac.length > 0) {
                cipherStream.write(mac);
            }
        } finally {
            IOHelper.close(cipherStream, "cipher", LOG);
            IOHelper.close(plaintextStream, "plaintext", LOG);
        }
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) DataInputStream(java.io.DataInputStream) CipherInputStream(javax.crypto.CipherInputStream) InputStream(java.io.InputStream) Key(java.security.Key)

Example 2 with CipherOutputStream

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

the class CipherOutputStream1Test method testFlush.

/**
     * flush() method testing. Tests that method flushes the data to the
     * underlying output stream.
     */
public void testFlush() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestOutputStream tos = new TestOutputStream();
    CipherOutputStream cos = new CipherOutputStream(tos) {
    };
    cos.write(data);
    cos.flush();
    byte[] result = tos.toByteArray();
    if (!Arrays.equals(result, data)) {
        fail("CipherOutputStream did not flush the data.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream)

Example 3 with CipherOutputStream

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

the class CipherOutputStream1Test method testWrite3.

/**
     * write(byte[] b, int off, int len) method testing.
     */
public void testWrite3() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestOutputStream tos = new TestOutputStream();
    CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
    for (int i = 0; i < data.length; i++) {
        cos.write(data, i, 1);
    }
    cos.flush();
    byte[] result = tos.toByteArray();
    if (!Arrays.equals(result, data)) {
        fail("CipherOutputStream wrote incorrect data.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) NullCipher(javax.crypto.NullCipher)

Example 4 with CipherOutputStream

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

the class CipherOutputStream1Test method testCipherOutputStream.

/**
     * CipherOutputStream(OutputStream os) method testing. Tests that
     * CipherOutputStream uses NullCipher if Cipher is not specified
     * in the constructor.
     */
public void testCipherOutputStream() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestOutputStream tos = new TestOutputStream();
    CipherOutputStream cos = new CipherOutputStream(tos) {
    };
    cos.write(data);
    cos.flush();
    byte[] result = tos.toByteArray();
    if (!Arrays.equals(result, data)) {
        fail("NullCipher should be used " + "if Cipher is not specified.");
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream)

Example 5 with CipherOutputStream

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

the class CipherOutputStream1Test method test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher.

public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    Key key = kg.generateKey();
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key);
    CipherOutputStream cos = new CipherOutputStream(baos, c);
    assertNotNull(cos);
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) SecureRandom(java.security.SecureRandom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Aggregations

CipherOutputStream (javax.crypto.CipherOutputStream)35 Cipher (javax.crypto.Cipher)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 IOException (java.io.IOException)10 CipherInputStream (javax.crypto.CipherInputStream)7 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 NullCipher (javax.crypto.NullCipher)5 IvParameterSpec (javax.crypto.spec.IvParameterSpec)5 FileOutputStream (java.io.FileOutputStream)4 SecretKey (javax.crypto.SecretKey)4 DataOutputStream (java.io.DataOutputStream)3 OutputStream (java.io.OutputStream)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 KeyGenerator (javax.crypto.KeyGenerator)3 HTTPMessageHeader (APJP.HTTP11.HTTPMessageHeader)2 HTTPRequestMessage (APJP.HTTP11.HTTPRequestMessage)2 HTTPResponseMessage (APJP.HTTP11.HTTPResponseMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2