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

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

Introduction

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

Prototype

public PaddedBufferedBlockCipher(BlockCipher cipher) 

Source Link

Document

Create a buffered block cipher PKCS7 padding

Usage

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

License:Apache License

@SuppressWarnings("deprecation")
@Test//  w w  w. ja va 2 s  .  co m
public void AesBlockStream() throws IOException {
    byte[] originalBytes = "hello, my name is inigo montoya".getBytes();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(originalBytes);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

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

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

    byte[] key = new byte[32];
    byte[] iv = new byte[16];

    // note: the IV needs to be VERY unique!
    rand.nextBytes(key); // 256bit key
    rand.nextBytes(iv); // 128bit block size

    boolean success = CryptoAES.encryptStream(aesEngine, key, iv, inputStream, outputStream, logger);

    if (!success) {
        fail("crypto was not successful");
    }

    byte[] encryptBytes = outputStream.toByteArray();

    inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    outputStream = new ByteArrayOutputStream();

    success = CryptoAES.decryptStream(aesEngine, key, iv, inputStream, outputStream, logger);

    if (!success) {
        fail("crypto was not successful");
    }

    byte[] decryptBytes = outputStream.toByteArray();

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

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

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

License:Apache License

@SuppressWarnings("deprecation")
@Test/*  w  ww.  j a  v a 2  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");
    }
}

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

License:Apache License

@SuppressWarnings("deprecation")
@Test/*from ww w  . ja  va 2s. c o m*/
public void AesWithIVBlockStream() throws IOException {
    byte[] originalBytes = "hello, my name is inigo montoya".getBytes();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(originalBytes);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

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

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

    byte[] key = new byte[32];
    byte[] iv = new byte[16];

    // note: the IV needs to be VERY unique!
    rand.nextBytes(key); // 256bit key
    rand.nextBytes(iv); // 128bit block size

    boolean success = CryptoAES.encryptStreamWithIV(aesEngine, key, iv, inputStream, outputStream, logger);

    if (!success) {
        fail("crypto was not successful");
    }

    byte[] encryptBytes = outputStream.toByteArray();

    inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    outputStream = new ByteArrayOutputStream();

    success = CryptoAES.decryptStreamWithIV(aesEngine, key, inputStream, outputStream, logger);

    if (!success) {
        fail("crypto was not successful");
    }

    byte[] decryptBytes = outputStream.toByteArray();

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

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

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

License:Apache License

@Test
public void EccAesMode() throws IOException {
    // test AES encrypt mode
    SecureRandom secureRandom = new SecureRandom();

    AsymmetricCipherKeyPair key1 = CryptoECC.generateKeyPair(CryptoECC.default_curve, secureRandom);
    AsymmetricCipherKeyPair key2 = CryptoECC.generateKeyPair(CryptoECC.default_curve, secureRandom);

    PaddedBufferedBlockCipher aesEngine1 = new PaddedBufferedBlockCipher(
            new CBCBlockCipher(new AESFastEngine()));
    PaddedBufferedBlockCipher aesEngine2 = new PaddedBufferedBlockCipher(
            new CBCBlockCipher(new AESFastEngine()));

    IESWithCipherParameters cipherParams = CryptoECC.generateSharedParametersWithCipher(secureRandom);

    IESEngine encrypt = CryptoECC.createEngine(aesEngine1);
    IESEngine decrypt = CryptoECC.createEngine(aesEngine2);

    // note: we want an ecc key that is AT LEAST 512 bits! (which is equal to AES 256)
    // using 521 bits from curve.
    CipherParameters private1 = key1.getPrivate();
    CipherParameters public1 = key1.getPublic();

    CipherParameters private2 = key2.getPrivate();
    CipherParameters public2 = key2.getPublic();

    byte[] message = Hex.decode(
            "123456784358754934597967249867359283792374987692348750276509765091834790abcdef123456784358754934597967249867359283792374987692348750276509765091834790abcdef123456784358754934597967249867359283792374987692348750276509765091834790abcdef");

    // test stream mode
    byte[] encrypted = CryptoECC.encrypt(encrypt, private1, public2, cipherParams, message, logger);
    byte[] plaintext = CryptoECC.decrypt(decrypt, private2, public1, cipherParams, encrypted, logger);

    if (Arrays.equals(encrypted, message)) {
        fail("stream cipher test failed");
    }/*  ww w . ja v  a2  s  .  c om*/

    if (!Arrays.equals(plaintext, message)) {
        fail("stream cipher test failed");
    }
}

From source file:edu.tamu.tcat.crypto.bouncycastle.SymmetricCipherBuilderImpl.java

License:Apache License

@Override
public SymmetricCipher buildCipher(Cipher cipher, Mode mode, boolean encryption, byte[] key, byte[] iv)
        throws CipherException {
    ParametersWithIV cipherParameters = new ParametersWithIV(new KeyParameter(key), iv);
    BlockCipher underlyingCipher = null;
    switch (cipher) {
    case AES128:/*from w ww.  j  a va2s  .  com*/
    case AES192:
    case AES256:
        underlyingCipher = new AESEngine();
        break;
    }
    BlockCipher blockCipher = null;
    switch (mode) {
    case CBC:
        blockCipher = new CBCBlockCipher(underlyingCipher);
        break;
    case GCM:
        throw new CipherException("GCM mode is authenticating encryption; use buildAEADCipher instead");
    }
    BufferedBlockCipher bufferedBlockCipher = new PaddedBufferedBlockCipher(blockCipher);
    bufferedBlockCipher.init(encryption, cipherParameters);

    return new BouncyCastleBlockCipher(bufferedBlockCipher);
}

From source file:edu.wisc.doit.tcrypt.AbstractPublicKeyEncrypter.java

License:Apache License

protected BufferedBlockCipher createBlockCipher() {
    return new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
}

From source file:EncriptDecriptArchivo.TestBouncy.java

License:Open Source License

public byte[] Encrypt(String keys, byte[] plainText) {
    byte[] key = keys.getBytes();
    byte[] ptBytes = plainText;
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
    cipher.init(true, new KeyParameter(key));
    byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)];
    int tam = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0);
    try {//from w ww .  j av  a 2  s.  com
        cipher.doFinal(rv, tam);
    } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ce) {
        ce.printStackTrace();
    }
    return rv;
}

From source file:EncriptDecriptArchivo.TestBouncy.java

License:Open Source License

public byte[] Decrypt(String key2, byte[] cipherText) {
    byte[] key = key2.getBytes();
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
    cipher.init(false, new KeyParameter(key));
    byte[] rv = new byte[cipher.getOutputSize(cipherText.length)];
    int tam = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0);
    try {/* w ww  . ja  v  a2s  .  co  m*/
        cipher.doFinal(rv, tam);
    } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ce) {
        ce.printStackTrace();
    }
    return rv;
}

From source file:ezbake.crypto.TDESCrypto.java

License:Apache License

public TDESCrypto(byte[] key) {
    encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
    decryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());

    encryptCipher.init(true, new KeyParameter(key));
    decryptCipher.init(false, new KeyParameter(key));

    this.key = key;
}

From source file:ezbake.crypto.TDESCryptoTest.java

License:Apache License

@Test
public void test() throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    PaddedBufferedBlockCipher encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
    PaddedBufferedBlockCipher decryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());

    byte inBuff[] = "Hello Wd".getBytes();
    byte[] outBuff = new byte[512];
    byte[] keyBytes = "TestTestTestTest".getBytes();
    byte[] uncipherData = new byte[8];

    encryptCipher.init(true, new KeyParameter(keyBytes));
    decryptCipher.init(false, new KeyParameter(keyBytes));

    encryptCipher.processBytes(inBuff, 0, inBuff.length, outBuff, 0);
    encryptCipher.doFinal(outBuff, 0);/*  w ww  .j  a v  a  2  s .  c  o m*/

    decryptCipher.processBytes(outBuff, 0, 2 * inBuff.length, uncipherData, 0);
    decryptCipher.doFinal(uncipherData, 0);

    log.debug("Uncipher Data: {}", uncipherData);

    assertTrue("Hello Wd".equals(new String(uncipherData)));
}