Example usage for org.bouncycastle.crypto PBEParametersGenerator init

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

Introduction

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

Prototype

public void init(byte[] password, byte[] salt, int iterationCount) 

Source Link

Document

initialise the PBE generator.

Usage

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

License:Open Source License

/**
 * SecureCrypto Constructor./*from   ww  w  . ja v  a  2 s  .co m*/
 * 
 * @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:com.wlami.mibox.core.encryption.PBKDF2.java

License:Open Source License

protected static byte[] doPbkdf2(String password, String salt, int rounds) {
    PBEParametersGenerator pbeParametersGenerator = new PKCS5S2ParametersGenerator();
    pbeParametersGenerator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password.toCharArray()),
            PBEParametersGenerator.PKCS5PasswordToBytes(salt.toCharArray()), rounds);
    KeyParameter keyParameter = (KeyParameter) pbeParametersGenerator
            .generateDerivedParameters(DEFAULT_KEY_LENGTH_BIT);
    return keyParameter.getKey();
}

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

License:Open Source License

/**
 *
 * @param password/*from ww w. j a  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:dorkbox.util.crypto.Crypto.java

License:Apache License

/**
 * Secure way to generate an AES key based on a password.
 *
 * @param password/*from   w w  w  .jav  a2s.c o m*/
 *                 The password that you want to mix
 * @param salt
 *                 should be a RANDOM number, at least 256bits (32 bytes) in size.
 * @param iterationCount
 *                 should be a lot, like 10,000
 *
 * @return the secure key to use
 */
public static byte[] PBKDF2(byte[] password, byte[] salt, int iterationCount) {
    SHA256Digest digest = new SHA256Digest();
    PBEParametersGenerator pGen = new PKCS5S2ParametersGenerator(digest);
    pGen.init(password, salt, iterationCount);

    KeyParameter key = (KeyParameter) pGen.generateDerivedMacParameters(digest.getDigestSize() * 8); // *8 for bit length.

    // zero out the password.
    Arrays.fill(password, (byte) 0);

    return key.getKey();
}

From source file:edu.vt.middleware.crypt.pbe.AbstractPKCSKeyGenerator.java

License:Open Source License

/** {@inheritDoc} */
public byte[] generate(final char[] password, final int size) {
    if (size < 1) {
        throw new IllegalArgumentException("Size must be positive integer.");
    }/*w  ww  .ja  v a 2s.  c o m*/

    final PBEParametersGenerator generator = newParamGenerator();
    generator.init(toBytes(password), salt, iterationCount);

    final KeyParameter p = (KeyParameter) generator.generateDerivedParameters(size);
    return p.getKey();
}

From source file:edu.vt.middleware.crypt.PbeKeyGenerator.java

License:Open Source License

/**
 * Generate an encryption key from a password using the given parameter
 * generator./*from w ww . j  a va 2s.  c  om*/
 *
 * @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  salt  Key initialization data.
 *
 * @return  Secret key derived from password using PBE key generation method.
 */
private SecretKey generate(final PBEParametersGenerator generator, final byte[] password,
        final int keyBitLength, final byte[] salt) {
    generator.init(password, salt, iterations);

    final KeyParameter keyParam = (KeyParameter) generator.generateDerivedParameters(keyBitLength);
    final SecretKeySpec spec = new SecretKeySpec(keyParam.getKey(), symmetricAlg.getAlgorithm());
    return spec;
}

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 ww w.  ja  v a2s  .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());
}

From source file:org.jruby.ext.openssl.PKCS5.java

License:Open Source License

private static RubyString generatePBEKey(final Ruby runtime, final char[] pass, final byte[] salt,
        final int iter, final int keySize) {
    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(pass), salt, iter);
    CipherParameters params = generator.generateDerivedParameters(keySize * 8);
    return StringHelper.newString(runtime, ((KeyParameter) params).getKey());
}

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

private static CipherParameters extractPBES2CipherParams(char[] password, PBES2Parameters pbeParams) {
    PBKDF2Params pbkdfParams = PBKDF2Params.getInstance(pbeParams.getKeyDerivationFunc().getParameters());
    int keySize = 192;
    if (pbkdfParams.getKeyLength() != null) {
        keySize = pbkdfParams.getKeyLength().intValue() * 8;
    }//from   www.  ja va 2s. c  om
    int iterationCount = pbkdfParams.getIterationCount().intValue();
    byte[] salt = pbkdfParams.getSalt();
    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, iterationCount);
    return generator.generateDerivedParameters(keySize);
}