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));
}
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;
}
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));
}
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;
}
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);
}
Aggregations