Example usage for org.bouncycastle.crypto PBEParametersGenerator generateDerivedParameters

List of usage examples for org.bouncycastle.crypto PBEParametersGenerator generateDerivedParameters

Introduction

In this page you can find the example usage for org.bouncycastle.crypto PBEParametersGenerator generateDerivedParameters.

Prototype

public abstract CipherParameters generateDerivedParameters(int keySize, int ivSize);

Source Link

Document

generate derived parameters for a key of length keySize, and an initialisation vector (IV) of length ivSize.

Usage

From source file:com.password.locker.crypto.SecureCryptoImpl.java

License:Open Source License

/**
 * SecureCrypto Constructor./*w ww.  ja  v a2s.c  om*/
 * 
 * @param password
 *       password for the crypto keyspec.
 * 
 * @throws InvalidAlgorithmParameterException 
 * @throws InvalidKeyException 
 * @throws NoSuchPaddingException 
 * @throws NoSuchProviderException 
 * @throws NoSuchAlgorithmException 
 */
public SecureCryptoImpl(final char[] password) throws InvalidKeyException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException {

    SHA256Digest digest = new SHA256Digest();

    String s = Constants.PROPERTIES.getStringProperty(Constants.SALT_KEY, PasswordUtils.getSalt(digest));
    salt = Hex.decode(s);
    if (salt.length != digest.getDigestSize()) {
        LOGGER.warn("Warning salt size is not the size of the Digest.");
    }

    //---------------------------------------------------
    // Setup encryption.
    //---------------------------------------------------
    PBEParametersGenerator pGen = new PKCS12ParametersGenerator(digest);

    pGen.init(PBEParametersGenerator.PKCS12PasswordToBytes(password), salt, ITERATIONS);

    ParametersWithIV params = (ParametersWithIV) pGen.generateDerivedParameters(KEY_LEN, IV_LEN);

    SecretKeySpec encKey = new SecretKeySpec(((KeyParameter) params.getParameters()).getKey(), "AES");

    encryption = Cipher.getInstance(Constants.CRYPTO_ALGORITHM, new BouncyCastleProvider());

    encryption.init(Cipher.ENCRYPT_MODE, encKey, new IvParameterSpec(params.getIV()));

    //---------------------------------------------------
    // Setup decryption.
    //---------------------------------------------------

    decryption = Cipher.getInstance(Constants.CRYPTO_SEC_KEY_SPEC, new BouncyCastleProvider());

    PBEKeySpec keySpec = new PBEKeySpec(password, salt, ITERATIONS);
    SecretKeyFactory fact = SecretKeyFactory.getInstance(Constants.CRYPTO_SEC_KEY_SPEC,
            new BouncyCastleProvider());

    try {
        decryption.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec));
    } catch (InvalidKeySpecException e) {
        ExceptionUtils.fatalError(SecureCryptoImpl.class, e);
    }
    Constants.PROPERTIES.addProperty(Constants.SALT_KEY, s);
}

From source file:com.thecorpora.qbo.androidapk.AESCipher.java

License:Open Source License

private ParametersWithIV getKeyParamWithIv(String keyphrase, byte[] salt) {
    int iterationCount = 1;
    //creating generator for PBE derived keys and ivs as used by open ssl
    PBEParametersGenerator generator = new OpenSSLPBEParametersGenerator();

    //intialse the PBE generator with password, salt and iteration count
    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(keyphrase.toCharArray()), salt, iterationCount);

    //Generate a key with initialisation vector parameter derived from the password, salt and iteration count
    ParametersWithIV paramWithIv = (ParametersWithIV) generator.generateDerivedParameters(256, 128);
    KeyParameter keyParam = (KeyParameter) paramWithIv.getParameters();

    return paramWithIv;
}

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

License:Open Source License

/**
 *
 * @param password//from   w w w  .j  a va2s  . co 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:edu.vt.middleware.crypt.PbeKeyGenerator.java

License:Open Source License

/**
 * Generate an encryption key/IV pair from a password using the given
 * parameter generator.//from   w w w.j  av a 2  s.  c o m
 *
 * @param  generator  Key generator for specific PBE method.
 * @param  password  Password as byte array (depends on PBE method).
 * @param  keyBitLength  Size of generated key in bits.
 * @param  ivBitLength  Size of generated IV in bits.
 * @param  salt  Key initialization data.
 *
 * @return  Secret key derived from password using PBE key generation method.
 */
private KeyWithIV generate(final PBEParametersGenerator generator, final byte[] password,
        final int keyBitLength, final int ivBitLength, final byte[] salt) {
    generator.init(password, salt, iterations);

    final ParametersWithIV params = (ParametersWithIV) generator.generateDerivedParameters(keyBitLength,
            ivBitLength);
    final KeyParameter keyParam = (KeyParameter) params.getParameters();
    return new KeyWithIV(new SecretKeySpec(keyParam.getKey(), symmetricAlg.getAlgorithm()), params.getIV());
}