1

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Naveen Kumar
  • 141
  • 3
  • 11
  • Did you try using what this thread recommends? https://stackoverflow.com/questions/18244630/elliptic-curve-with-digital-signature-algorithm-ecdsa-implementation-on-bouncy – Khojiakbar Mar 12 '19 at 14:21

3 Answers3

2

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.

clauub
  • 1,134
  • 9
  • 19
  • But as I mentioned earlier, I don't want to go to the Spongy-castle provider. Is there anyway to get rid of this. – Naveen Kumar Mar 13 '19 at 05:23
  • @clauub, since some Android bouncycastle package has been renamed and there is no more conflict by adding it as a separate package. See example here: https://github.com/serso/web3a/blob/4dda74db948f8cbd9a79ba4b9ab456316ea52c4d/app/src/main/java/org/solovyev/android/web3a/App.java#L47 – Joe Dow Sep 15 '22 at 15:24
1

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;
Naveen Kumar
  • 141
  • 3
  • 11
0

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

Antonio Altieri
  • 131
  • 1
  • 7
  • Is it possible to manually import the Bouncy-castle library in android Studio, with another name. By downloading the jar file from official BC website. ?? – Naveen Kumar Mar 13 '19 at 05:25
  • @NaveenKumar Do you support android 3.0 or below? If not then you can import bouncycastle as usual library and use it. Bouncycastle's package bundled in android was renamed from org.bouncycastle to com.android.org.bouncycastle, so it doesn't conflict with the newest version of the library. – Khojiakbar Mar 13 '19 at 12:49
  • Since some Android bouncycastle package has been renamed and there is no more conflict by adding it as a separate package. See example here: https://github.com/serso/web3a/blob/4dda74db948f8cbd9a79ba4b9ab456316ea52c4d/app/src/main/java/org/solovyev/android/web3a/App.java#L47 – Joe Dow Sep 15 '22 at 15:26