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:org.universAAL.ri.gateway.communication.cipher.Blowfish.java

License:Apache License

public Blowfish(final String encryptionKey) {
    cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new BlowfishEngine()));
    key = new KeyParameter(encryptionKey.getBytes());
}

From source file:org.votingsystem.signature.util.Encryptor.java

License:Open Source License

public static String encryptAES(String messageToEncrypt, AESParams aesParams) throws NoSuchPaddingException,
        NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException,
        IllegalBlockSizeException, UnsupportedEncodingException, InvalidCipherTextException {
    PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
    KeyParameter keyParam = new KeyParameter(aesParams.getKey().getEncoded());
    ParametersWithIV params = new ParametersWithIV(keyParam, aesParams.getIV().getIV());
    pbbc.init(true, params); //to decrypt put param to false
    byte[] input = messageToEncrypt.getBytes("UTF-8");
    byte[] output = new byte[pbbc.getOutputSize(input.length)];
    int bytesWrittenOut = pbbc.processBytes(input, 0, input.length, output, 0);
    pbbc.doFinal(output, bytesWrittenOut);
    return new String(org.bouncycastle.util.encoders.Base64.encode(output));
}

From source file:org.votingsystem.signature.util.Encryptor.java

License:Open Source License

public static String decryptAES(String messageToDecrypt, AESParams aesParams) throws NoSuchPaddingException,
        NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException,
        IllegalBlockSizeException, UnsupportedEncodingException, InvalidCipherTextException {
    PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
    KeyParameter keyParam = new KeyParameter(aesParams.getKey().getEncoded());
    CipherParameters params = new ParametersWithIV(keyParam, aesParams.getIV().getIV());
    pbbc.init(false, params); //to encrypt put param to true
    byte[] input = org.bouncycastle.util.encoders.Base64.decode(messageToDecrypt.getBytes("UTF-8"));
    byte[] output = new byte[pbbc.getOutputSize(input.length)];
    int bytesWrittenOut = pbbc.processBytes(input, 0, input.length, output, 0);
    pbbc.doFinal(output, bytesWrittenOut);
    int i = output.length - 1; //remove padding
    while (i >= 0 && output[i] == 0) {
        --i;//from w w  w . j a  v  a2  s .  c om
    }
    return new String(Arrays.copyOf(output, i + 1), "UTF-8");
}

From source file:org.xmind.core.internal.security.BouncyCastleSecurityProvider.java

License:Open Source License

private BufferedBlockCipher createCipher(boolean encrypt, IEncryptionData encData, String password)
        throws CoreException {
    checkEncryptionData(encData);//from   w  w w  .j a  va 2s  .  co  m

    // Create a parameter generator
    PKCS12ParametersGenerator paramGen = new PKCS12ParametersGenerator(new MD5Digest());

    // Get the password bytes
    byte[] pwBytes = password == null ? new byte[0]
            : PBEParametersGenerator.PKCS12PasswordToBytes(password.toCharArray());

    // Initialize the parameter generator with password bytes, 
    // salt and iteration counts
    paramGen.init(pwBytes, getSalt(encData), getIterationCount(encData));

    // Generate a parameter
    CipherParameters param = paramGen.generateDerivedParameters(128);

    // Create a block cipher
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));

    // Initialize the block cipher
    cipher.init(encrypt, param);
    return cipher;
}

From source file:org.xwiki.crypto.cipher.internal.symmetric.factory.AbstractBcCbcPaddedCipherFactory.java

License:Open Source License

@Override
protected BlockCipher getCipherInstance(boolean forEncryption, SymmetricCipherParameters parameters) {
    return new CBCBlockCipher(getEngineInstance());
}

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

License:Open Source License

/**
 * {@inheritDoc}/* w  ww  .  j ava2s .  c o  m*/
 *
 * @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}/*  w  ww  .  j  ava2 s .  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:piecework.security.concrete.ExampleBouncyCastleEncryptionService.java

License:Educational Community License

private BufferedBlockCipher cipher() {
    BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new PKCS7Padding());
    cipher.reset();//from www . ja va 2s.c o m
    return cipher;
}

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 v  a 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;/*ww  w  .j a  v a 2 s.  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;
}