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:com.android.isoma.enc.Encryption.java

License:Open Source License

public synchronized byte[] encrypt(byte[] data)
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    byte[] iv = null;
    if (data == null || data.length == 0) {
        return new byte[0];
    }/*  w  ww.j ava 2  s. c  o m*/
    BufferedBlockCipher encryptcipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new ZeroBytePadding());

    String source = "00000000";
    iv = source.getBytes();

    CipherParameters ivAndKey = new ParametersWithIV(key, iv);
    encryptcipher.init(true, ivAndKey);

    return callCipher(encryptcipher, data);
}

From source file:com.android.isoma.enc.Encryption.java

License:Open Source License

public synchronized byte[] decrypt(byte[] data)
        throws DataLengthException, IllegalStateException, InvalidCipherTextException {
    byte[] iv = null;
    if (data == null || data.length == 0) {
        return new byte[0];
    }/*from   ww  w  .j  av  a  2 s.co m*/

    BufferedBlockCipher decryptcipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new ZeroBytePadding());

    String source = "00000000";
    iv = source.getBytes();

    CipherParameters ivAndKey = new ParametersWithIV(key, iv);

    decryptcipher.init(false, ivAndKey);

    return callCipher(decryptcipher, data);
}

From source file:com.completetrsst.crypto.Crypto.java

License:Apache License

private static byte[] _cryptIES(byte[] input, Key recipient, boolean forEncryption)
        throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    IESCipher cipher = new IESCipher(new IESEngine(new ECDHBasicAgreement(),
            new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA256Digest()),
            new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()))));

    cipher.engineInit(forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, recipient, new SecureRandom());
    return cipher.engineDoFinal(input, 0, input.length);
}

From source file:com.completetrsst.crypto.Crypto.java

License:Apache License

private static byte[] _cryptBytesAES(byte[] input, byte[] key, boolean forEncryption)
        throws InvalidCipherTextException {
    assert key.length == 32; // 32 bytes == 256 bits
    return process(input, new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())),
            new KeyParameter(key), forEncryption);
    // note: using zero IV because we generate a new key for every message
}

From source file:com.foilen.smalltools.crypt.symmetric.AESCrypt.java

License:Open Source License

@Override
protected BufferedBlockCipher generateBufferedBlockCipher() {
    return new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
}

From source file:com.friendconnect.utils.Encrypter.java

License:Open Source License

public Encrypter() {
    RijndaelEngine engine = new RijndaelEngine();
    CBCBlockCipher c = new CBCBlockCipher(engine);
    cipher = new PaddedBufferedBlockCipher(c);
}

From source file:com.giacomodrago.immediatecrypt.aes.AESFacadeImpl.java

License:Open Source License

@Override
public AESEncryptedMessage encrypt(byte[] plaintext, String password) throws EncryptionException {

    // Check password is not empty
    if (password.isEmpty()) {
        throw new EncryptionException("Password is empty.");
    }//from  www  .  java2 s  . c om

    // Generate random password salt
    String salt = RandomStringUtils.randomAlphanumeric(SALT_LENGTH);

    ParametersWithIV params = createEncryptionParameters(password, salt);
    byte[] iv = params.getIV();

    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));

    cipher.init(true, params);

    byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)];

    int outputLen = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext, 0);
    try {
        cipher.doFinal(ciphertext, outputLen);
    } catch (DataLengthException ex) {
        throw new EncryptionException(ex);
    } catch (IllegalStateException ex) {
        throw new EncryptionException(ex);
    } catch (InvalidCipherTextException ex) {
        throw new EncryptionException(ex);
    }

    return new AESEncryptedMessage(salt, iv, ciphertext);

}

From source file:com.giacomodrago.immediatecrypt.aes.AESFacadeImpl.java

License:Open Source License

@Override
public byte[] decrypt(AESEncryptedMessage encryptedMessage, String password) throws EncryptionException {

    byte[] ciphertext = encryptedMessage.getCiphertext();
    String salt = encryptedMessage.getSalt();
    byte[] iv = encryptedMessage.getIv();

    ParametersWithIV params = createDecryptionParameters(password, salt, iv);

    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));

    try {/*from ww w. j  a v a2  s  .  c  o  m*/
        cipher.init(false, params);
    } catch (IllegalArgumentException ex) {
        throw new EncryptionException(ex);
    }

    byte[] plaintext = new byte[cipher.getOutputSize(ciphertext.length)];

    int outputLen = cipher.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0);
    try {
        cipher.doFinal(plaintext, outputLen);
    } catch (DataLengthException ex) {
        throw new EncryptionException(ex);
    } catch (IllegalStateException ex) {
        throw new EncryptionException(ex);
    } catch (InvalidCipherTextException ex) {
        throw new EncryptionException(ex);
    }

    return plaintext;

}

From source file:com.github.flbaue.jcrypttool.v1.DecryptionRunnable.java

License:Apache License

@Override
public void run() {
    PaddedBufferedBlockCipher cipher;//from w ww . java2  s . co m
    byte[] key;
    byte[] iv;
    byte[] salt;

    try (InputStream fileInputStream = new BufferedInputStream(
            new FileInputStream(encryptionSettings.inputFile))) {

        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        salt = extractSalt(fileInputStream);
        iv = extractIV(fileInputStream);
        key = generateKey(encryptionSettings.password, salt);
        KeyParameter keyParam = new KeyParameter(key);
        CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(false, params);

        /*
        System.out.println(getClass().getName() + " salt:\t" + Base64.toBase64String(salt) + " (" + salt.length + " byte)");
        System.out.println(getClass().getName() + " key:\t" + Base64.toBase64String(key) + " (" + key.length + " byte)");
        System.out.println(getClass().getName() + " iv:\t\t" + Base64.toBase64String(iv) + " (" + iv.length + " byte)");
        */

        InputStream in = null;
        OutputStream out = null;
        try {
            in = new GZIPInputStream(new CipherInputStream(fileInputStream, cipher));
            out = new BufferedOutputStream(new FileOutputStream(encryptionSettings.outputFile));
            processStreams(in, out);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeStream(in);
            closeStream(out);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.flbaue.jcrypttool.v1.EncryptionRunnable.java

License:Apache License

@Override
public void run() {
    progress.start();//  ww  w  . ja  va2  s .com

    final byte[] salt = generateSalt();
    final byte[] key = generateKey(encryptionSettings.password, salt);
    final byte[] iv;
    final PaddedBufferedBlockCipher cipher;

    try (OutputStream fileOutputStream = new BufferedOutputStream(
            new FileOutputStream(encryptionSettings.outputFile))) {

        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        iv = generateIV();
        final KeyParameter keyParam = new KeyParameter(key);
        final CipherParameters params = new ParametersWithIV(keyParam, iv);
        cipher.init(true, params);

        /*
        System.out.println(getClass().getName() + " salt:\t" + Base64.toBase64String(salt) + " (" + salt.length + " byte)");
        System.out.println(getClass().getName() + " key:\t" + Base64.toBase64String(key) + " (" + key.length + " byte)");
        System.out.println(getClass().getName() + " iv:\t\t" + Base64.toBase64String(iv) + " (" + iv.length + " byte)");
        */

        InputStream in = null;
        OutputStream out = null;
        try {
            writeInitBlock(fileOutputStream, salt, iv);
            in = new BufferedInputStream(new FileInputStream(encryptionSettings.inputFile));
            out = new GZIPOutputStream(new CipherOutputStream(fileOutputStream, cipher));
            processStreams(in, out);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            closeStream(in);
            closeStream(out);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    progress.setFinished();
}