java.security.NoSuchAlgorithmException: no such algorithm: ECDSA for provider BC

9.9k Views Asked by At

I am trying to generate a secp256k1 keypair with KeyPairGenerator function. My function looks like

public fun generateSECP256K1Keypair():KeyPair{
  Security.addProvider(org.bouncycastle.jce.provider.BouncyCastleProvider())
  var keypairGen: KeyPairGenerator = KeyPairGenerator.getInstance("ECDSA","BC")
  val spec:ECGenParameterSpec = ECGenParameterSpec("secp256k1")
  keypairGen.initialize(spec, SecureRandom())
  var keyPair:KeyPair= keypairGen.genKeyPair()
  return keyPair;

}

My gradle file dependencies look like this

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'org.bouncycastle:bcprov-jdk15on:1.61'

}

When I try to execute this function, I am getting following error

Process: com.example.myapplication, PID: 6477
java.lang.RuntimeException: Unable to create service com.example.myapplication.service.Myservice: java.security.NoSuchAlgorithmException: no such algorithm: ECDSA for provider BC
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3201)
    at android.app.ActivityThread.-wrap5(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
    at android.os.Handler.dispatchMessage(Handler.java:102)

If I use, SpongyCastle instead of BouncyCastle, It working fine. But I don't want to use SpongyCastle provider in my case.

3

There are 3 best solutions below

0
Naveen Kumar On BEST ANSWER

This code works fine for me

public fun generateSECP256K1Keypair():KeyPair{
  Security.addProvider(org.bouncycastle.jce.provider.BouncyCastleProvider())
  var keypairGen: KeyPairGenerator = KeyPairGenerator.getInstance("EC","BC")
  val spec:ECGenParameterSpec = ECGenParameterSpec("secp256k1")
  keypairGen.initialize(spec, SecureRandom())
  var keyPair:KeyPair= keypairGen.genKeyPair()
  return keyPair;
3
clauub On

I found this, read this.

Android support for ECDSA was introduced since version 4.0 using Bouncycastle (v1.46) as the default cryptographic provider. See the blog https://nelenkov.blogspot.com.es/2011/12/using-ecdh-on-android.html?m=1

But Android included a shortened version of Bouncycastle, and there is no full support for ECDSA. You can see in the link that algorithm KeyPairGenerator/ECDSA is not supported, which is the required one to generate ethereum keys.

You can not include directly the bouncycastle library because there is a conflict with the package name org.bouncycastle. I suggest to include spongycastle in your project, which it is a repackaged version of bouncycastle for Android org.spongycastle.

The package name conflict has been resolved in new android versions, but if your target are old versions then you need to ensure which cryptographic provider is being used.

3
Antonio Altieri On

There is a package name conflict between bouncycastle and spongycastle.

Spongycastle is a recompiled and tested version of BouncyCastle. If you really want to use Bouncycastle, i think that the only solution is to include the entire source code of bouncycastle inside your app, renaming packages names