Search in sources :

Example 6 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class PEMUtilities method getKey.

private static SecretKey getKey(char[] password, String algorithm, int keyLength, byte[] salt, boolean des2) throws IOException {
    OpenSSLPBEParametersGenerator pGen = new OpenSSLPBEParametersGenerator();
    pGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt);
    KeyParameter keyParam;
    keyParam = (KeyParameter) pGen.generateDerivedParameters(keyLength * 8);
    byte[] key = keyParam.getKey();
    if (des2 && key.length >= 24) {
        // For DES2, we must copy first 8 bytes into the last 8 bytes.
        System.arraycopy(key, 0, key, 16, 8);
    }
    return new javax.crypto.spec.SecretKeySpec(key, algorithm);
}
Also used : KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter) OpenSSLPBEParametersGenerator(org.gudy.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator)

Example 7 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class PKCS12ParametersGenerator method generateDerivedParameters.

/**
 * Generate a key with initialisation vector parameter derived from
 * the password, salt, and iteration count we are currently initialised
 * with.
 *
 * @param keySize the size of the key we want (in bits)
 * @param ivSize the size of the iv we want (in bits)
 * @return a ParametersWithIV object.
 */
@Override
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
    keySize = keySize / 8;
    ivSize = ivSize / 8;
    byte[] dKey = generateDerivedKey(KEY_MATERIAL, keySize);
    byte[] iv = generateDerivedKey(IV_MATERIAL, ivSize);
    return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), iv, 0, ivSize);
}
Also used : ParametersWithIV(org.gudy.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter)

Example 8 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class OpenSSLPBEParametersGenerator method generateDerivedParameters.

/**
 * Generate a key with initialisation vector parameter derived from
 * the password, salt, and iteration count we are currently initialised
 * with.
 *
 * @param keySize the size of the key we want (in bits)
 * @param ivSize the size of the iv we want (in bits)
 * @return a ParametersWithIV object.
 * @exception IllegalArgumentException if keySize + ivSize is larger than the base hash size.
 */
@Override
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
    keySize = keySize / 8;
    ivSize = ivSize / 8;
    byte[] dKey = generateDerivedKey(keySize + ivSize);
    return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
}
Also used : ParametersWithIV(org.gudy.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter)

Example 9 with KeyParameter

use of org.gudy.bouncycastle.crypto.params.KeyParameter in project BiglyBT by BiglySoftware.

the class PKCS5S1ParametersGenerator method generateDerivedParameters.

/**
 * Generate a key with initialisation vector parameter derived from
 * the password, salt, and iteration count we are currently initialised
 * with.
 *
 * @param keySize the size of the key we want (in bits)
 * @param ivSize the size of the iv we want (in bits)
 * @return a ParametersWithIV object.
 * @exception IllegalArgumentException if keySize + ivSize is larger than the base hash size.
 */
@Override
public CipherParameters generateDerivedParameters(int keySize, int ivSize) {
    keySize = keySize / 8;
    ivSize = ivSize / 8;
    if ((keySize + ivSize) > digest.getDigestSize()) {
        throw new IllegalArgumentException("Can't generate a derived key " + (keySize + ivSize) + " bytes long.");
    }
    byte[] dKey = generateDerivedKey();
    return new ParametersWithIV(new KeyParameter(dKey, 0, keySize), dKey, keySize, ivSize);
}
Also used : ParametersWithIV(org.gudy.bouncycastle.crypto.params.ParametersWithIV) KeyParameter(org.gudy.bouncycastle.crypto.params.KeyParameter)

Aggregations

KeyParameter (org.gudy.bouncycastle.crypto.params.KeyParameter)9 CipherParameters (org.gudy.bouncycastle.crypto.CipherParameters)3 RC4Engine (org.gudy.bouncycastle.crypto.engines.RC4Engine)3 ParametersWithIV (org.gudy.bouncycastle.crypto.params.ParametersWithIV)3 IESWithCipherParameters (org.gudy.bouncycastle.crypto.params.IESWithCipherParameters)2 KDFParameters (org.gudy.bouncycastle.crypto.params.KDFParameters)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 OpenSSLPBEParametersGenerator (org.gudy.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator)1