androidkotlinbouncycastlespongycastle

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


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.


Solution

  • 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;