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:org.xwiki.crypto.passwd.internal.AESPasswordCiphertext.java

License:Open Source License

/**
 * {@inheritDoc}/*from ww  w  .  j  av a  2 s  .c om*/
 *
 * @see org.xwiki.crypto.passwd.internal.CAST5PasswordCiphertext#newCipherInstance()
 */
@Override
protected PaddedBufferedBlockCipher newCipherInstance() {
    return new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
}

From source file:org.xwiki.crypto.passwd.internal.CAST5PasswordCiphertext.java

License:Open Source License

/**
 * {@inheritDoc}/*from   ww w .j  a v a  2s. c  o m*/
 *
 * @see org.xwiki.crypto.passwd.internal.AbstractPasswordCiphertext#newCipherInstance()
 */
@Override
protected PaddedBufferedBlockCipher newCipherInstance() {
    return new PaddedBufferedBlockCipher(new CBCBlockCipher(new CAST5Engine()));
}

From source file:pl.sind.keepass.crypto.BcAESCipher.java

License:Apache License

public byte[] decrypt(byte[] key, byte[] data, byte[] iv) throws CipherException {
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));

    if (iv != null) {
        cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
    } else {//from w  ww .  j a va  2 s .  c o  m
        cipher.init(false, new KeyParameter(key));
    }

    byte[] decoded = new byte[cipher.getOutputSize(data.length)];

    int out = cipher.processBytes(data, 0, data.length, decoded, 0);
    try {
        out += cipher.doFinal(decoded, out);

        if (out < decoded.length) {
            decoded = Arrays.copyOf(decoded, out);
        }

    } catch (DataLengthException e) {
        // we are padding so shouldn happen
        throw new CipherException("Invalid data lenght", e);
    } catch (IllegalStateException e) {
        throw new CipherException("Decrypting error", e);
    } catch (InvalidCipherTextException e) {
        throw new CipherException("Unable to decrypt data", e);
    }

    return decoded;

}

From source file:pl.sind.keepass.crypto.BcAESCipher.java

License:Apache License

public byte[] encrypt(byte[] key, byte[] data, byte[] iv, int rounds, boolean padding) throws CipherException {
    BufferedBlockCipher cipher = null;/*from   w  w w  . j  av  a  2s  .c  o m*/

    if (padding) {
        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
    } else {
        cipher = new BufferedBlockCipher(new AESEngine());
    }

    if (iv != null) {
        cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
    } else {
        cipher.init(true, new KeyParameter(key));
    }

    byte[] encoded = null;
    if (padding) {
        encoded = new byte[cipher.getOutputSize(data.length)];
    } else {
        encoded = new byte[data.length];
    }

    int out = cipher.processBytes(data, 0, data.length, encoded, 0);
    if (rounds > 1) {
        for (int i = 1; i < rounds; i++) {
            out = cipher.processBytes(encoded, 0, encoded.length, encoded, 0);
        }
    }

    try {
        if (padding && out < encoded.length)
            cipher.doFinal(encoded, out);
    } catch (DataLengthException e) {
        // we are padding so shouldn happen
        throw new CipherException("Invalid data lenght", e);
    } catch (IllegalStateException e) {
        throw new CipherException("Decrypting error", e);
    } catch (InvalidCipherTextException e) {
        throw new CipherException("Unable to decrypt data", e);
    }

    return encoded;
}