Example usage for org.bouncycastle.crypto.modes GCMBlockCipher getUnderlyingCipher

List of usage examples for org.bouncycastle.crypto.modes GCMBlockCipher getUnderlyingCipher

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.modes GCMBlockCipher getUnderlyingCipher.

Prototype

public BlockCipher getUnderlyingCipher() 

Source Link

Usage

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

License:Apache License

@Test
public void AesWithIVGcm() throws IOException {
    byte[] bytes = "hello, my name is inigo montoya".getBytes();

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

    GCMBlockCipher aesEngine = new GCMBlockCipher(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);/*from   w w w  .  ja v a  2  s.  c  o  m*/
    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");
    }
}