Example usage for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher getUnderlyingCipher

List of usage examples for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher getUnderlyingCipher

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.paddings PaddedBufferedBlockCipher getUnderlyingCipher.

Prototype

public BlockCipher getUnderlyingCipher() 

Source Link

Document

return the cipher this object wraps.

Usage

From source file:dorkbox.util.crypto.AesTest.java

License:Apache License

@SuppressWarnings("deprecation")
@Test/*from ww w .j a v  a2 s .  c o m*/
public void AesWithIVBlock() throws IOException {
    byte[] bytes = "hello, my name is inigo montoya".getBytes();

    SecureRandom rand = new SecureRandom(entropySeed.getBytes());

    PaddedBufferedBlockCipher aesEngine = new PaddedBufferedBlockCipher(
            new CBCBlockCipher(new AESFastEngine()));

    byte[] key = new byte[32]; // 256bit key
    byte[] iv = new byte[aesEngine.getUnderlyingCipher().getBlockSize()];

    // note: the IV needs to be VERY unique!
    rand.nextBytes(key);
    rand.nextBytes(iv);

    byte[] encryptAES = CryptoAES.encryptWithIV(aesEngine, key, iv, bytes, logger);
    byte[] decryptAES = CryptoAES.decryptWithIV(aesEngine, key, encryptAES, logger);

    if (Arrays.equals(bytes, encryptAES)) {
        fail("bytes should not be equal");
    }

    if (!Arrays.equals(bytes, decryptAES)) {
        fail("bytes not equal");
    }
}