Example usage for org.bouncycastle.crypto PBEParametersGenerator PKCS5PasswordToBytes

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

Introduction

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

Prototype

public static byte[] PKCS5PasswordToBytes(char[] password) 

Source Link

Document

converts a password to a byte array according to the scheme in PKCS5 (ascii, no padding)

Usage

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:edu.vt.middleware.crypt.pbe.OpenSSLKeyGenerator.java

License:Open Source License

/** {@inheritDoc} */
public byte[] generate(final char[] password, final int size) {
    final OpenSSLPBEParametersGenerator generator = new OpenSSLPBEParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt);

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

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

License:Open Source License

/** {@inheritDoc} */
protected byte[] toBytes(final char[] password) {
    return PBEParametersGenerator.PKCS5PasswordToBytes(password);
}

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

License:Open Source License

/**
 * Generate a key from a text password using the PKCS#5 version 1 method
 * described at http://www.rsa.com/rsalabs/node.asp?id=2127.
 *
 * @param  password  Raw material used for key generation.
 * @param  keyBitLength  Size of generated key in bits.
 * @param  digest  Digest algorithm to use during key generation.
 * @param  salt  Key initialization data.
 *
 * @return  Secret key based on password.
 *//* w  w w .ja  v  a2  s  .co m*/
public SecretKey generatePkcs5v1(final char[] password, final int keyBitLength, final DigestAlgorithm digest,
        final byte[] salt) {
    final int minSize = keyBitLength / BITS_IN_BYTE;
    if (digest.getDigest().getDigestSize() < minSize) {
        throw new IllegalArgumentException("Digest is too small for chosen key size. "
                + "Use a digest whose size is at least " + minSize + " bytes.");
    }
    return generate(new PKCS5S1ParametersGenerator(digest.getDigest()),
            PBEParametersGenerator.PKCS5PasswordToBytes(password), keyBitLength, salt);
}

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

License:Open Source License

/**
 * Generate a key/IV pair from a text password using the PKCS#5 version 1
 * method described at http://www.rsa.com/rsalabs/node.asp?id=2127.
 *
 * @param  password  Raw material used for key generation.
 * @param  keyBitLength  Size of generated key in bits.
 * @param  ivBitLength  Size of generated IV in bits.
 * @param  digest  Digest algorithm to use during key generation.
 * @param  salt  Key initialization data.
 *
 * @return  Secret key based on password.
 *//*  www.ja  v  a  2 s .  c o  m*/
public KeyWithIV generatePkcs5v1(final char[] password, final int keyBitLength, final int ivBitLength,
        final DigestAlgorithm digest, final byte[] salt) {
    final int minSize = (keyBitLength + ivBitLength) / BITS_IN_BYTE;
    if (digest.getDigest().getDigestSize() < minSize) {
        throw new IllegalArgumentException("Digest is too small for chosen key + IV size. "
                + "Use a digest whose size is at least " + minSize + " bytes.");
    }
    return generate(new PKCS5S1ParametersGenerator(digest.getDigest()),
            PBEParametersGenerator.PKCS5PasswordToBytes(password), keyBitLength, ivBitLength, salt);
}

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

License:Open Source License

/**
 * Generate a key from a text password using the PKCS#5 version 2.0 method
 * described at http://www.rsa.com/rsalabs/node.asp?id=2127. A SHA-1 digest is
 * used as the calculation function.//from w  w w . j  a  v a2s  .c om
 *
 * @param  password  Raw material used for key generation.
 * @param  keyBitLength  Size of generated key in bits.
 * @param  salt  Key initialization data.
 *
 * @return  Secret key based on password.
 */
public SecretKey generatePkcs5v2(final char[] password, final int keyBitLength, final byte[] salt) {
    return generate(new PKCS5S2ParametersGenerator(), PBEParametersGenerator.PKCS5PasswordToBytes(password),
            keyBitLength, salt);
}

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

License:Open Source License

/**
 * Generate a key/IV pair from a text password using the PKCS#5 version 2.0
 * method described at http://www.rsa.com/rsalabs/node.asp?id=2127. A SHA-1
 * digest is used as the calculation function.
 *
 * @param  password  Raw material used for key generation.
 * @param  keyBitLength  Size of generated key in bits.
 * @param  ivBitLength  Size of generated IV in bits.
 * @param  salt  Key initialization data.
 *
 * @return  Secret key based on password.
 *//*from w  ww . j a v a 2 s  .  c o  m*/
public KeyWithIV generatePkcs5v2(final char[] password, final int keyBitLength, final int ivBitLength,
        final byte[] salt) {
    return generate(new PKCS5S2ParametersGenerator(), PBEParametersGenerator.PKCS5PasswordToBytes(password),
            keyBitLength, ivBitLength, salt);
}

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

License:Open Source License

/**
 * Generate a key from a text password using a method based ok PKCS#5 version
 * 2 that is consistent with that performed by the openssl enc operation.
 *
 * @param  password  Raw material used for key generation.
 * @param  keyBitLength  Size of generated key in bits.
 * @param  salt  Key initialization data.
 *
 * @return  Secret key based on password.
 *///from w  w  w  .  j  av a2 s  .  co m
public SecretKey generateOpenssl(final char[] password, final int keyBitLength, final byte[] salt) {
    return generate(new OpenSSLPBEParametersGenerator(), PBEParametersGenerator.PKCS5PasswordToBytes(password),
            keyBitLength, salt);
}

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

License:Open Source License

/**
 * Generate a key/IV pair from a text password using a strategy compatible
 * with the OpenSSL enc operation. The strategy is based on PKCS#5 version 2,
 * but uses a MD5 hash instead of SHA1 and an iteration count of 1. For
 * compatibility with OpenSSL, the IV size should be equal to key size.
 *
 * @param  password  Raw material used for key generation.
 * @param  keyBitLength  Size of generated key in bits.
 * @param  ivBitLength  Size of generated IV in bits.
 * @param  salt  Key initialization data.
 *
 * @return  Secret key based on password.
 *///  w  ww .j  a v a2s. c o m
public KeyWithIV generateOpenssl(final char[] password, final int keyBitLength, final int ivBitLength,
        final byte[] salt) {
    return generate(new OpenSSLPBEParametersGenerator(), PBEParametersGenerator.PKCS5PasswordToBytes(password),
            keyBitLength, ivBitLength, salt);
}