Java 类org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator 实例源码

项目:Direct-File-Downloader    文件:PEMUtilities.java   
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);
}
项目:Aki-SSL    文件:PEMUtilities.java   
private static KeyParameter getKey(
    char[]  password,
    int     keyLength,
    byte[]  salt,
    boolean des2)
    throws PEMException
{
    PBEParametersGenerator paramsGen = new OpenSSLPBEParametersGenerator();

    paramsGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt, 1);

    KeyParameter kp = (KeyParameter)paramsGen.generateDerivedParameters(keyLength * 8);

    if (des2 && kp.getKey().length == 24)
    {
        // For DES2, we must copy first 8 bytes into the last 8 bytes.
        byte[] key = kp.getKey();

        System.arraycopy(key, 0, key, 16, 8);

        return new KeyParameter(key);
    }

    return kp;
}
项目:AcademicTorrents-Downloader    文件:PEMUtilities.java   
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);
}
项目:irma_future_id    文件:PEMUtilities.java   
private static SecretKey getKey(
    char[]  password,
    String  algorithm,
    int     keyLength,
    byte[]  salt,
    boolean des2)
{
    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 SecretKeySpec(key, algorithm);
}
项目:irma_future_id    文件:PEMReader.java   
private static SecretKey getKey(
    char[] password,
    String algorithm,
    int keyLength,
    byte[] salt,
    boolean des2)
{
    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);
}
项目:bc-java    文件:PEMUtilities.java   
private static SecretKey getKey(
    char[]  password,
    String  algorithm,
    int     keyLength,
    byte[]  salt,
    boolean des2)
{
    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 SecretKeySpec(key, algorithm);
}
项目:bc-java    文件:PEMReader.java   
private static SecretKey getKey(
    char[] password,
    String algorithm,
    int keyLength,
    byte[] salt,
    boolean des2)
{
    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);
}
项目:Aki-SSL    文件:OpenSSLPBKDF.java   
protected SecretKey engineGenerateSecret(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof PBEKeySpec)
    {
        PBEKeySpec pbeSpec = (PBEKeySpec)keySpec;

        if (pbeSpec.getSalt() == null)
        {
            throw new InvalidKeySpecException("missing required salt");
        }

        if (pbeSpec.getIterationCount() <= 0)
        {
            throw new InvalidKeySpecException("positive iteration count required: "
                + pbeSpec.getIterationCount());
        }

        if (pbeSpec.getKeyLength() <= 0)
        {
            throw new InvalidKeySpecException("positive key length required: "
                + pbeSpec.getKeyLength());
        }

        if (pbeSpec.getPassword().length == 0)
        {
            throw new IllegalArgumentException("password empty");
        }

        OpenSSLPBEParametersGenerator pGen = new OpenSSLPBEParametersGenerator();

        pGen.init(Strings.toByteArray(pbeSpec.getPassword()), pbeSpec.getSalt());

        return new SecretKeySpec(((KeyParameter)pGen.generateDerivedParameters(pbeSpec.getKeyLength())).getKey(), "OpenSSLPBKDF");
    }

    throw new InvalidKeySpecException("Invalid KeySpec");
}
项目:Java-Security    文件:OpenSSLKeyGenerator.java   
/** {@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();
}
项目:CryptMeme    文件:PBETest.java   
public void performTest()
    throws Exception
{
    byte[] salt = new byte[16];
    int    iCount = 100;

    for (int i = 0; i != salt.length; i++)
    {
        salt[i] = (byte)i;
    }

    OpenSSLPBEParametersGenerator   pGen = new OpenSSLPBEParametersGenerator();

    pGen.init(
            PBEParametersGenerator.PKCS5PasswordToBytes(password),
            salt,
            iCount);

    ParametersWithIV params = (ParametersWithIV)pGen.generateDerivedParameters(keySize, ivSize);

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

    Cipher          c;

    if (baseAlgorithm.equals("RC4"))
    {
        c = Cipher.getInstance(baseAlgorithm, "BC");

        c.init(Cipher.ENCRYPT_MODE, encKey);
    }
    else
    {
        c = Cipher.getInstance(baseAlgorithm + "/CBC/PKCS7Padding", "BC");

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

    byte[]          enc = c.doFinal(salt);

    c = Cipher.getInstance(algorithm, "BC");

    PBEKeySpec          keySpec = new PBEKeySpec(password, salt, iCount);
    SecretKeyFactory    fact = SecretKeyFactory.getInstance(algorithm, "BC");

    c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec));

    byte[]          dec = c.doFinal(enc);

    if (!Arrays.areEqual(salt, dec))
    {
        fail("" + algorithm + "failed encryption/decryption test");
    }
}
项目:irma_future_id    文件:PBETest.java   
public void performTest()
    throws Exception
{
    byte[] salt = new byte[16];
    int    iCount = 100;

    for (int i = 0; i != salt.length; i++)
    {
        salt[i] = (byte)i;
    }

    OpenSSLPBEParametersGenerator   pGen = new OpenSSLPBEParametersGenerator();

    pGen.init(
            PBEParametersGenerator.PKCS5PasswordToBytes(password),
            salt,
            iCount);

    ParametersWithIV params = (ParametersWithIV)pGen.generateDerivedParameters(keySize, ivSize);

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

    Cipher          c;

    if (baseAlgorithm.equals("RC4"))
    {
        c = Cipher.getInstance(baseAlgorithm, "BC");

        c.init(Cipher.ENCRYPT_MODE, encKey);
    }
    else
    {
        c = Cipher.getInstance(baseAlgorithm + "/CBC/PKCS7Padding", "BC");

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

    byte[]          enc = c.doFinal(salt);

    c = Cipher.getInstance(algorithm, "BC");

    PBEKeySpec          keySpec = new PBEKeySpec(password, salt, iCount);
    SecretKeyFactory    fact = SecretKeyFactory.getInstance(algorithm, "BC");

    c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec));

    byte[]          dec = c.doFinal(enc);

    if (!Arrays.areEqual(salt, dec))
    {
        fail("" + algorithm + "failed encryption/decryption test");
    }
}
项目:irma_future_id    文件:PBETest.java   
public TestResult perform()
{
    byte[] salt = new byte[16];
    int    iCount = 100;

    for (int i = 0; i != salt.length; i++)
    {
        salt[i] = (byte)i;
    }

    try
    {
        OpenSSLPBEParametersGenerator   pGen = new OpenSSLPBEParametersGenerator();

        pGen.init(
                PBEParametersGenerator.PKCS5PasswordToBytes(password),
                salt);

        ParametersWithIV params = (ParametersWithIV)pGen.generateDerivedParameters(keySize, ivSize);
        SecretKeySpec    encKey = new SecretKeySpec(((KeyParameter)params.getParameters()).getKey(), baseAlgorithm);

        Cipher          c;

        if (baseAlgorithm.equals("RC4"))
        {
            c = Cipher.getInstance(baseAlgorithm, "BC");

            c.init(Cipher.ENCRYPT_MODE, encKey);
        }
        else
        {
            c = Cipher.getInstance(baseAlgorithm + "/CBC/PKCS7Padding", "BC");

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

        byte[]              enc = c.doFinal(salt);

        c = Cipher.getInstance(algorithm, "BC");

        PBEKeySpec          keySpec = new PBEKeySpec(password);
        SecretKeyFactory    fact = SecretKeyFactory.getInstance(algorithm, "BC");

        c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec), new PBEParameterSpec(salt, iCount));

        byte[]          dec = c.doFinal(enc);

        if (!arrayEquals(salt, dec))
        {
            return new SimpleTestResult(false, getName() + ": " + algorithm + "failed encryption/decryption test");
        }

        return new SimpleTestResult(true, getName() + ": Okay");
    }
    catch (Exception e)
    {
        return new SimpleTestResult(false, getName() + ": " + algorithm + " failed - exception " + e, e);
    }
}
项目:bc-java    文件:PBETest.java   
public void performTest()
    throws Exception
{
    byte[] salt = new byte[16];
    int    iCount = 100;

    for (int i = 0; i != salt.length; i++)
    {
        salt[i] = (byte)i;
    }

    OpenSSLPBEParametersGenerator   pGen = new OpenSSLPBEParametersGenerator();

    pGen.init(
            PBEParametersGenerator.PKCS5PasswordToBytes(password),
            salt,
            iCount);

    ParametersWithIV params = (ParametersWithIV)pGen.generateDerivedParameters(keySize, ivSize);

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

    Cipher          c;

    if (baseAlgorithm.equals("RC4"))
    {
        c = Cipher.getInstance(baseAlgorithm, "BC");

        c.init(Cipher.ENCRYPT_MODE, encKey);
    }
    else
    {
        c = Cipher.getInstance(baseAlgorithm + "/CBC/PKCS7Padding", "BC");

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

    byte[]          enc = c.doFinal(salt);

    c = Cipher.getInstance(algorithm, "BC");

    PBEKeySpec          keySpec = new PBEKeySpec(password, salt, iCount);
    SecretKeyFactory    fact = SecretKeyFactory.getInstance(algorithm, "BC");

    c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec));

    byte[]          dec = c.doFinal(enc);

    if (!Arrays.areEqual(salt, dec))
    {
        fail("" + algorithm + "failed encryption/decryption test");
    }
}
项目:bc-java    文件:PBETest.java   
public TestResult perform()
{
    byte[] salt = new byte[16];
    int    iCount = 100;

    for (int i = 0; i != salt.length; i++)
    {
        salt[i] = (byte)i;
    }

    try
    {
        OpenSSLPBEParametersGenerator   pGen = new OpenSSLPBEParametersGenerator();

        pGen.init(
                PBEParametersGenerator.PKCS5PasswordToBytes(password),
                salt);

        ParametersWithIV params = (ParametersWithIV)pGen.generateDerivedParameters(keySize, ivSize);
        SecretKeySpec    encKey = new SecretKeySpec(((KeyParameter)params.getParameters()).getKey(), baseAlgorithm);

        Cipher          c;

        if (baseAlgorithm.equals("RC4"))
        {
            c = Cipher.getInstance(baseAlgorithm, "BC");

            c.init(Cipher.ENCRYPT_MODE, encKey);
        }
        else
        {
            c = Cipher.getInstance(baseAlgorithm + "/CBC/PKCS7Padding", "BC");

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

        byte[]              enc = c.doFinal(salt);

        c = Cipher.getInstance(algorithm, "BC");

        PBEKeySpec          keySpec = new PBEKeySpec(password);
        SecretKeyFactory    fact = SecretKeyFactory.getInstance(algorithm, "BC");

        c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec), new PBEParameterSpec(salt, iCount));

        byte[]          dec = c.doFinal(enc);

        if (!arrayEquals(salt, dec))
        {
            return new SimpleTestResult(false, getName() + ": " + algorithm + "failed encryption/decryption test");
        }

        return new SimpleTestResult(true, getName() + ": Okay");
    }
    catch (Exception e)
    {
        return new SimpleTestResult(false, getName() + ": " + algorithm + " failed - exception " + e, e);
    }
}
项目:droidklavier    文件:Crypto.java   
private static ParametersWithIV getKeyParamWithIv(String keyphrase, byte[] salt, int aes_bit) {
  int iterationCount = 1;
  PBEParametersGenerator generator = new OpenSSLPBEParametersGenerator();
  generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(keyphrase.toCharArray()), salt, iterationCount);
  return (ParametersWithIV) generator.generateDerivedParameters(aes_bit, 128);
}