use of org.spongycastle.crypto.params.DHPublicKeyParameters in project Zom-Android by zom.
the class OtrCryptoEngineImpl method generateDHKeyPair.
public KeyPair generateDHKeyPair() throws OtrCryptoException {
// Generate a AsymmetricCipherKeyPair using BC.
DHParameters dhParams = new DHParameters(MODULUS, GENERATOR, null, DH_PRIVATE_KEY_MINIMUM_BIT_LENGTH);
DHKeyGenerationParameters params = new DHKeyGenerationParameters(new SecureRandom(), dhParams);
DHKeyPairGenerator kpGen = new DHKeyPairGenerator();
kpGen.init(params);
AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();
// Convert this AsymmetricCipherKeyPair to a standard JCE KeyPair.
DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
try {
KeyFactory keyFac = KeyFactory.getInstance("DH");
DHPublicKeySpec pubKeySpecs = new DHPublicKeySpec(pub.getY(), MODULUS, GENERATOR);
DHPublicKey pubKey = (DHPublicKey) keyFac.generatePublic(pubKeySpecs);
DHParameters dhParameters = priv.getParameters();
DHPrivateKeySpec privKeySpecs = new DHPrivateKeySpec(priv.getX(), dhParameters.getP(), dhParameters.getG());
DHPrivateKey privKey = (DHPrivateKey) keyFac.generatePrivate(privKeySpecs);
return new KeyPair(pubKey, privKey);
} catch (Exception e) {
throw new OtrCryptoException(e);
}
}
Aggregations