Example usage for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher

List of usage examples for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher

Introduction

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

Prototype

public CBCBlockCipher(BlockCipher cipher) 

Source Link

Document

Basic constructor.

Usage

From source file:de.gs_sys.kp2016.crypto.SymmetricCipher.java

License:Open Source License

public static boolean decryptFile(byte[] password, String filenameIn, String filenameOut)
        throws DecryptionException {

    byte[] key = extendKey(password);

    try (InputStream inputStream = new FileInputStream(filenameIn)) {
        OutputStream outputStream = new FileOutputStream(filenameOut);

        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),
                new ZeroBytePadding());
        cipher.init(false, new KeyParameter(key));

        byte[] buffer = new byte[BUFFER_SPACE];
        int readSize;
        int writeSize;

        byte[] out;

        while ((readSize = inputStream.read(buffer)) != -1) {
            writeSize = cipher.getOutputSize(buffer.length);
            out = new byte[writeSize];

            writeSize = cipher.processBytes(buffer, 0, readSize, out, 0);
            outputStream.write(out, 0, writeSize);
        }// www.j a va 2 s .c  o  m
        outputStream.flush();
        try {
            out = new byte[1024];
            writeSize = cipher.doFinal(out, 0);
            outputStream.write(out, 0, writeSize);
            outputStream.flush();
        } catch (Exception ce) {
            ce.printStackTrace();
            throw new DecryptionException(SymmetricCipher.class.getName());
        }

        outputStream.close();
        inputStream.close();
        return true;
    } catch (FileNotFoundException e) {

    } catch (IOException e1) {

    }
    return false;
}

From source file:de.gs_sys.kp2016.crypto.SymmetricCipher.java

License:Open Source License

public static boolean encryptFile(byte[] password, String filenameIn, String filenameOut) {

    byte[] key = extendKey(password);

    try (InputStream inputStream = new FileInputStream(filenameIn)) {
        OutputStream outputStream = new FileOutputStream(filenameOut);

        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),
                new ZeroBytePadding());
        cipher.init(true, new KeyParameter(key));

        byte[] buffer = new byte[BUFFER_SPACE];
        int readSize;
        int writeSize;

        byte[] out;

        while ((readSize = inputStream.read(buffer)) != -1) {
            writeSize = cipher.getOutputSize(buffer.length);
            out = new byte[writeSize];

            writeSize = cipher.processBytes(buffer, 0, readSize, out, 0);

            outputStream.write(out, 0, writeSize);
        }/*from   w  w w .j  ava2s  .  c  om*/

        try {
            out = new byte[1024];
            writeSize = cipher.doFinal(out, 0);
            outputStream.write(out, 0, writeSize);
            outputStream.flush();
        } catch (Exception ce) {
            ce.printStackTrace();
            return false;
        }
        outputStream.close();
        inputStream.close();
        return true;
    } catch (FileNotFoundException e) {

    } catch (IOException e1) {

    }
    return false;
}

From source file:de.gs_sys.kp2016.crypto.SymmetricCipher.java

License:Open Source License

public static byte[] encrypt(byte[] key, byte[] in) {
    key = extendKey(key);//w  w  w. j  av  a2s  .co m

    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
    cipher.init(true, new KeyParameter(key));
    byte[] out = new byte[cipher.getOutputSize(in.length)];
    int length = cipher.processBytes(in, 0, in.length, out, 0);
    try {
        cipher.doFinal(out, length);
    } catch (Exception ce) {
        ce.printStackTrace();
        return null;
    }
    return out;
}

From source file:de.gs_sys.kp2016.crypto.SymmetricCipher.java

License:Open Source License

/**
 * May Trim 0-bytes at the end, witch are needed !
 * @param key//from  w  ww .  j  ava 2 s  . c  o m
 * @param cipherText
 */
public static byte[] decrypt(byte[] key, byte[] cipherText) throws DecryptionException {
    key = extendKey(key);

    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
    cipher.init(false, new KeyParameter(key));
    byte[] out = new byte[cipher.getOutputSize(cipherText.length)];
    int length = cipher.processBytes(cipherText, 0, cipherText.length, out, 0);
    try {
        cipher.doFinal(out, length);
    } catch (Exception ce) {
        // ce.printStackTrace();
        throw new DecryptionException(SymmetricCipher.class.getName());
    }

    int t = -1;
    for (int i = out.length - 1; i > 0; i--) {
        if (out[i] == 0)
            t = i;
        else
            break;
    }

    if (t >= 0) {
        byte[] out2 = new byte[t];
        System.arraycopy(out, 0, out2, 0, t);
        return out2;
    }

    return out;
}

From source file:de.jpm.model.EncryptionService.java

License:Open Source License

/**
 *
 * @param password/*from www .  ja  v a  2  s .  c o  m*/
 */
public void initCipher(char[] password) {
    PBEParametersGenerator keyGenerator = new PKCS12ParametersGenerator(new SHA256Digest());
    keyGenerator.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password), salt, 20);
    CipherParameters keyParams = keyGenerator.generateDerivedParameters(256, 128);

    encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    encryptCipher.init(true, keyParams);
    decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    decryptCipher.init(false, keyParams);
}

From source file:de.tntinteractive.portalsammler.engine.CryptoHelper.java

License:Open Source License

private static PaddedBufferedBlockCipher initAes(final byte[] key, final SecureRandom srand,
        final boolean forEncryption) {
    final CipherParameters cipherParams = new ParametersWithRandom(new KeyParameter(key), srand);

    final AESFastEngine aes = new AESFastEngine();
    final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(aes));
    cipher.init(forEncryption, cipherParams);
    return cipher;
}

From source file:de.tsenger.animamea.crypto.AmAESCrypto.java

License:Open Source License

private void initCiphers(byte[] key, byte[] iv) {

    // get the keyBytes
    keyBytes = new byte[key.length];
    System.arraycopy(key, 0, keyBytes, 0, key.length);

    keyP = new KeyParameter(keyBytes);

    // get the IV
    IV = new byte[blockSize];
    System.arraycopy(iv, 0, IV, 0, IV.length);

    // create the ciphers
    // AES block cipher in CBC mode with ISO7816d4 padding
    encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()),
            new ISO7816d4Padding());

    decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()),
            new ISO7816d4Padding());

    // create the IV parameter
    ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV);

    encryptCipher.init(true, parameterIV);
    decryptCipher.init(false, parameterIV);
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

private void initCiphers(byte[] key, byte[] iv) {
    // get the keyBytes
    keyBytes = new byte[key.length];
    System.arraycopy(key, 0, keyBytes, 0, key.length);

    // get the IV
    IV = new byte[blockSize];
    System.arraycopy(iv, 0, IV, 0, iv.length);

    keyP = new KeyParameter(keyBytes);

    encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()),
            new ISO7816d4Padding());
    decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()),
            new ISO7816d4Padding());

    // create the IV parameter
    ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV);

    encryptCipher.init(true, parameterIV);
    decryptCipher.init(false, parameterIV);
}

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

License:Apache License

@SuppressWarnings("deprecation")
@Test// ww w. ja v  a  2s  .  c  om
public void AesBlock() 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];
    byte[] iv = new byte[16];

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

    byte[] encryptAES = CryptoAES.encrypt(aesEngine, key, iv, bytes, logger);
    byte[] decryptAES = CryptoAES.decrypt(aesEngine, key, iv, 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 w  ww.jav  a  2 s .c o 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");
    }
}