Search in sources :

Example 1 with BetterCipherInputStream

use of com.keepassdroid.stream.BetterCipherInputStream in project KeePassDX by Kunzisoft.

the class CipherTest method testCipherStreams.

public void testCipherStreams() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {
    final int MESSAGE_LENGTH = 1024;
    byte[] key = new byte[32];
    byte[] iv = new byte[16];
    byte[] plaintext = new byte[MESSAGE_LENGTH];
    rand.nextBytes(key);
    rand.nextBytes(iv);
    rand.nextBytes(plaintext);
    CipherEngine aes = CipherFactory.getInstance(AesEngine.CIPHER_UUID);
    Cipher encrypt = aes.getCipher(Cipher.ENCRYPT_MODE, key, iv);
    Cipher decrypt = aes.getCipher(Cipher.DECRYPT_MODE, key, iv);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    CipherOutputStream cos = new CipherOutputStream(bos, encrypt);
    cos.write(plaintext);
    cos.close();
    byte[] secrettext = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(secrettext);
    BetterCipherInputStream cis = new BetterCipherInputStream(bis, decrypt);
    LEDataInputStream lis = new LEDataInputStream(cis);
    byte[] decrypttext = lis.readBytes(MESSAGE_LENGTH);
    assertArrayEquals("Encryption and decryption failed", plaintext, decrypttext);
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) BetterCipherInputStream(com.keepassdroid.stream.BetterCipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CipherEngine(com.keepassdroid.crypto.engine.CipherEngine) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LEDataInputStream(com.keepassdroid.stream.LEDataInputStream)

Aggregations

CipherEngine (com.keepassdroid.crypto.engine.CipherEngine)1 BetterCipherInputStream (com.keepassdroid.stream.BetterCipherInputStream)1 LEDataInputStream (com.keepassdroid.stream.LEDataInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Cipher (javax.crypto.Cipher)1 CipherOutputStream (javax.crypto.CipherOutputStream)1