Search in sources :

Example 1 with DHPublicKeyParameters

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);
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) DHPublicKeyParameters(org.spongycastle.crypto.params.DHPublicKeyParameters) DHParameters(org.spongycastle.crypto.params.DHParameters) DHPublicKey(javax.crypto.interfaces.DHPublicKey) DHPrivateKeyParameters(org.spongycastle.crypto.params.DHPrivateKeyParameters) DHKeyGenerationParameters(org.spongycastle.crypto.params.DHKeyGenerationParameters) SecureRandom(java.security.SecureRandom) DHKeyPairGenerator(org.spongycastle.crypto.generators.DHKeyPairGenerator) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) DHPrivateKeySpec(javax.crypto.spec.DHPrivateKeySpec) DHPublicKeySpec(javax.crypto.spec.DHPublicKeySpec) KeyFactory(java.security.KeyFactory)

Aggregations

IOException (java.io.IOException)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyFactory (java.security.KeyFactory)1 KeyPair (java.security.KeyPair)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SecureRandom (java.security.SecureRandom)1 DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)1 DHPublicKey (javax.crypto.interfaces.DHPublicKey)1 DHPrivateKeySpec (javax.crypto.spec.DHPrivateKeySpec)1 DHPublicKeySpec (javax.crypto.spec.DHPublicKeySpec)1 AsymmetricCipherKeyPair (org.spongycastle.crypto.AsymmetricCipherKeyPair)1 DHKeyPairGenerator (org.spongycastle.crypto.generators.DHKeyPairGenerator)1 DHKeyGenerationParameters (org.spongycastle.crypto.params.DHKeyGenerationParameters)1 DHParameters (org.spongycastle.crypto.params.DHParameters)1 DHPrivateKeyParameters (org.spongycastle.crypto.params.DHPrivateKeyParameters)1 DHPublicKeyParameters (org.spongycastle.crypto.params.DHPublicKeyParameters)1