Search in sources :

Example 1 with DHParametersGenerator

use of org.bouncycastle.crypto.generators.DHParametersGenerator in project robovm by robovm.

the class AlgorithmParameterGeneratorSpi method engineGenerateParameters.

protected AlgorithmParameters engineGenerateParameters() {
    DHParametersGenerator pGen = new DHParametersGenerator();
    if (random != null) {
        pGen.init(strength, 20, random);
    } else {
        pGen.init(strength, 20, new SecureRandom());
    }
    DHParameters p = pGen.generateParameters();
    AlgorithmParameters params;
    try {
        params = AlgorithmParameters.getInstance("DH", BouncyCastleProvider.PROVIDER_NAME);
        params.init(new DHParameterSpec(p.getP(), p.getG(), l));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return params;
}
Also used : DHParameters(org.bouncycastle.crypto.params.DHParameters) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) DHParametersGenerator(org.bouncycastle.crypto.generators.DHParametersGenerator) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 2 with DHParametersGenerator

use of org.bouncycastle.crypto.generators.DHParametersGenerator in project robovm by robovm.

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        Integer paramStrength = Integers.valueOf(strength);
        if (params.containsKey(paramStrength)) {
            param = (DHKeyGenerationParameters) params.get(paramStrength);
        } else {
            DHParameterSpec dhParams = BouncyCastleProvider.CONFIGURATION.getDHDefaultParameters(strength);
            if (dhParams != null) {
                param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));
            } else {
                synchronized (lock) {
                    // our key size.
                    if (params.containsKey(paramStrength)) {
                        param = (DHKeyGenerationParameters) params.get(paramStrength);
                    } else {
                        DHParametersGenerator pGen = new DHParametersGenerator();
                        pGen.init(strength, certainty, random);
                        param = new DHKeyGenerationParameters(random, pGen.generateParameters());
                        params.put(paramStrength, param);
                    }
                }
            }
        }
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
    DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCDHPublicKey(pub), new BCDHPrivateKey(priv));
}
Also used : KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) DHPublicKeyParameters(org.bouncycastle.crypto.params.DHPublicKeyParameters) DHParameters(org.bouncycastle.crypto.params.DHParameters) DHPrivateKeyParameters(org.bouncycastle.crypto.params.DHPrivateKeyParameters) DHKeyGenerationParameters(org.bouncycastle.crypto.params.DHKeyGenerationParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) DHParametersGenerator(org.bouncycastle.crypto.generators.DHParametersGenerator) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair)

Aggregations

DHParameterSpec (javax.crypto.spec.DHParameterSpec)2 DHParametersGenerator (org.bouncycastle.crypto.generators.DHParametersGenerator)2 DHParameters (org.bouncycastle.crypto.params.DHParameters)2 AlgorithmParameters (java.security.AlgorithmParameters)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 KeyPair (java.security.KeyPair)1 SecureRandom (java.security.SecureRandom)1 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)1 DHKeyGenerationParameters (org.bouncycastle.crypto.params.DHKeyGenerationParameters)1 DHPrivateKeyParameters (org.bouncycastle.crypto.params.DHPrivateKeyParameters)1 DHPublicKeyParameters (org.bouncycastle.crypto.params.DHPublicKeyParameters)1