Search in sources :

Example 1 with ElGamalParameters

use of com.github.zhenwei.core.crypto.params.ElGamalParameters in project LinLong-Java by zhenwei1108.

the class ElGamalKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
    ElGamalParameters egp = param.getParameters();
    DHParameters dhp = new DHParameters(egp.getP(), egp.getG(), null, egp.getL());
    BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
    BigInteger y = helper.calculatePublic(dhp, x);
    return new AsymmetricCipherKeyPair(new ElGamalPublicKeyParameters(y, egp), new ElGamalPrivateKeyParameters(x, egp));
}
Also used : ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) ElGamalPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters) BigInteger(java.math.BigInteger) ElGamalPublicKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 2 with ElGamalParameters

use of com.github.zhenwei.core.crypto.params.ElGamalParameters in project LinLong-Java by zhenwei1108.

the class KeyPairGeneratorSpi method initialize.

public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(params instanceof ElGamalParameterSpec) && !(params instanceof DHParameterSpec)) {
        throw new InvalidAlgorithmParameterException("parameter object not a DHParameterSpec or an ElGamalParameterSpec");
    }
    if (params instanceof ElGamalParameterSpec) {
        ElGamalParameterSpec elParams = (ElGamalParameterSpec) params;
        param = new ElGamalKeyGenerationParameters(random, new ElGamalParameters(elParams.getP(), elParams.getG()));
    } else {
        DHParameterSpec dhParams = (DHParameterSpec) params;
        param = new ElGamalKeyGenerationParameters(random, new ElGamalParameters(dhParams.getP(), dhParams.getG(), dhParams.getL()));
    }
    engine.init(param);
    initialised = true;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) ElGamalKeyGenerationParameters(com.github.zhenwei.core.crypto.params.ElGamalKeyGenerationParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) ElGamalParameterSpec(com.github.zhenwei.provider.jce.spec.ElGamalParameterSpec)

Example 3 with ElGamalParameters

use of com.github.zhenwei.core.crypto.params.ElGamalParameters in project LinLong-Java by zhenwei1108.

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        DHParameterSpec dhParams = WeGooProvider.CONFIGURATION.getDHDefaultParameters(strength);
        if (dhParams != null) {
            param = new ElGamalKeyGenerationParameters(random, new ElGamalParameters(dhParams.getP(), dhParams.getG(), dhParams.getL()));
        } else {
            ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
            pGen.init(strength, certainty, random);
            param = new ElGamalKeyGenerationParameters(random, pGen.generateParameters());
        }
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    ElGamalPublicKeyParameters pub = (ElGamalPublicKeyParameters) pair.getPublic();
    ElGamalPrivateKeyParameters priv = (ElGamalPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCElGamalPublicKey(pub), new BCElGamalPrivateKey(priv));
}
Also used : KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) ElGamalPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters) ElGamalKeyGenerationParameters(com.github.zhenwei.core.crypto.params.ElGamalKeyGenerationParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) ElGamalPublicKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters) ElGamalParametersGenerator(com.github.zhenwei.core.crypto.generators.ElGamalParametersGenerator) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 4 with ElGamalParameters

use of com.github.zhenwei.core.crypto.params.ElGamalParameters in project LinLong-Java by zhenwei1108.

the class AlgorithmParameterGeneratorSpi method engineGenerateParameters.

protected AlgorithmParameters engineGenerateParameters() {
    ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();
    if (random != null) {
        pGen.init(strength, 20, random);
    } else {
        pGen.init(strength, 20, CryptoServicesRegistrar.getSecureRandom());
    }
    ElGamalParameters p = pGen.generateParameters();
    AlgorithmParameters params;
    try {
        params = createParametersInstance("ElGamal");
        params.init(new DHParameterSpec(p.getP(), p.getG(), l));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return params;
}
Also used : ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) ElGamalParametersGenerator(com.github.zhenwei.core.crypto.generators.ElGamalParametersGenerator) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 5 with ElGamalParameters

use of com.github.zhenwei.core.crypto.params.ElGamalParameters in project LinLong-Java by zhenwei1108.

the class ElGamalParametersGenerator method generateParameters.

/**
 * which generates the p and g values from the given parameters, returning the ElGamalParameters
 * object.
 * <p>
 * Note: can take a while...
 *
 * @return a generated ElGamal parameters object.
 */
public ElGamalParameters generateParameters() {
    // 
    // find a safe prime p where p = 2*q + 1, where p and q are prime.
    // 
    BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random);
    BigInteger p = safePrimes[0];
    BigInteger q = safePrimes[1];
    BigInteger g = DHParametersHelper.selectGenerator(p, q, random);
    return new ElGamalParameters(p, g);
}
Also used : ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) BigInteger(java.math.BigInteger)

Aggregations

ElGamalParameters (com.github.zhenwei.core.crypto.params.ElGamalParameters)6 ElGamalPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters)3 BigInteger (java.math.BigInteger)3 DHParameterSpec (javax.crypto.spec.DHParameterSpec)3 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)2 ElGamalParametersGenerator (com.github.zhenwei.core.crypto.generators.ElGamalParametersGenerator)2 DHParameters (com.github.zhenwei.core.crypto.params.DHParameters)2 ElGamalKeyGenerationParameters (com.github.zhenwei.core.crypto.params.ElGamalKeyGenerationParameters)2 ElGamalPublicKeyParameters (com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)1 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)1 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)1 GOST3410PublicKeyAlgParameters (com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters)1 ElGamalParameter (com.github.zhenwei.core.asn1.oiw.ElGamalParameter)1 DHParameter (com.github.zhenwei.core.asn1.pkcs.DHParameter)1 RSAPrivateKey (com.github.zhenwei.core.asn1.pkcs.RSAPrivateKey)1