Skip to content Skip to sidebar Skip to footer

How Do I Get Ecdh Keypair In Android 9.0 Pie?

I want to get ECDH keypair (Public key and Private key). This method is not working in Android 9.0 pie, because Security provider 'BC' , 'SC' is removed from this version. I tried

Solution 1:

Try adding SpongyCastle manually:

Security.insertProviderAt(BouncyCastleProvider(), 1);

KeyPairGeneratorkeyGen= KeyPairGenerator.getInstance("EC");
SecureRandomrandom= SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(256, random);
KeyFactorykaif= KeyFactory.getInstance("EC");
KeyPairpair= keyGen.generateKeyPair();
PrivateKeyprivateKey= pair.getPrivate();
PublicKeypublicKey= pair.getPublic();

add this to your build.gradle dependencies:

/* spongy castle */
implementation "com.madgag.spongycastle:core:1.58.0.0"
implementation "com.madgag.spongycastle:prov:1.58.0.0"

Make sure the BouncyCastleProvider() was coming from spongycastle:

import org.spongycastle.jce.provider.BouncyCastleProvider

Solution 2:

One could also add BouncyCastleProvider alias bcprov-jdk15on:

dependencies {
    // https://mvnrepository.com/artifact/org.bouncycastle
    implementation "org.bouncycastle:bcprov-jdk15on:1.60"
    implementation "org.bouncycastle:bcpkix-jdk15on:1.60"
}

Solution 3:

Remove Provider ("BC") and insert BouncyCastle Manually

Security.removeProvider("BC");
Security.insertProviderAt(BouncyCastleProvider(), 1);

KeyPairGeneratorkeyGen= KeyPairGenerator.getInstance("EC");
SecureRandomrandom= SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(256, random);
KeyFactorykaif= KeyFactory.getInstance("EC");
KeyPairpair= keyGen.generateKeyPair();
PrivateKeyprivateKey= pair.getPrivate();
PublicKeypublicKey= pair.getPublic();

add this to your build.gradle dependencies:

/* Bouncy castle */
implementation 'org.bouncycastle:com.springsource.org.bouncycastle.jce:1.46.0'

Post a Comment for "How Do I Get Ecdh Keypair In Android 9.0 Pie?"