Skip to content Skip to sidebar Skip to footer

Ecdsa Keypairgenerator Not Available (but In Junit Works)

I am getting below Exception which I observed when running Android app - it does not occurs when I run the code below as JUnit. java.security.NoSuchAlgorithmException: ECDSA KeyPa

Solution 1:

The problem is that it is not possible to use BounceyCastle on Android - instead use SpongyCastle:

implementation 'com.madgag.spongycastle:prov:1.54.0.0'
implementation 'com.madgag.spongycastle:pkix:1.54.0.0'

And then initialize the provider with BouncyCastleProvider instance like below:

Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider())

Solution 2:

Latest version of BouncyCastle libraries can be used to generate keypairs using ECDSA algorithm on Android device with the below mentioned steps.

Include the latest bouncy castle library by adding the below dependencies.

    implementation "org.bouncycastle:bcprov-jdk15to18:1.68"
    implementation "org.bouncycastle:bcpkix-jdk15to18:1.68"

Note: Refer Provider, PKIX to get the latest version details.

After adding the libraries, follow any of the below mentioned approach based on the mentioned need.

Approach 1

If you want to use the provider entire application level, replace the Android OS Bouncycastle provider with the provider from the added library using below line.

// Remove the OS provided bouncy castle provider
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME)
// Add the bouncy castle provider from the added library
Security.addProvider(org.bouncycastle.jce.provider.BouncyCastleProvider())

Approach 2

If you don't want to replace the Provider throughout the application, you can pass the provider instance to the KeyPairGenerator like below.

Java

KeyPairGeneratorgenerator= KeyPairGenerator.getInstance("ECDSA", neworg.bouncycastle.jce.provider.BouncyCastleProvider())

Kotlin

valgenerator= KeyPairGenerator.getInstance("ECDSA", org.bouncycastle.jce.provider.BouncyCastleProvider())

Post a Comment for "Ecdsa Keypairgenerator Not Available (but In Junit Works)"