2

My app is minSDK 15 - so I thought I can use BouncyCastle directly. Unfortunately I get a NoSuchAlgorithmException. Do I have to use SpongyCastle then? The lib works great in a JVM app - but fails on android.

 Caused by: java.security.NoSuchAlgorithmException: no such algorithm: ECDSA for provider BC
    at sun.security.jca.GetInstance.getService(GetInstance.java:87)
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:206)
    at java.security.KeyPairGenerator.getInstance(KeyPairGenerator.java:307)
    at org.kethereum.crypto.Keys.createSecp256k1KeyPair$crypto(Keys.kt:43)
    at org.kethereum.crypto.Keys.createEcKeyPair(Keys.kt:51)
    at org.walleth.data.keystore.KethereumWallethKeyStore.newAddress(KethereumWallethKeyStore.kt:43)
    at org.walleth.activities.CreateAccountActivity$onCreate$4.onClick(CreateAccountActivity.kt:95)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
ligi
  • 39,001
  • 44
  • 144
  • 244

2 Answers2

5

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.

pedrofb
  • 37,271
  • 5
  • 94
  • 142
0

Had this issue in 2022 with modern Android version (API 30) and the latest web3j.

The issue has been solved by checking installation of bouncy castle and replace it with the fresh one.

private void setupBouncyCastle() {
    final Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
    if (provider == null) {
        // Web3j will set up the provider lazily when it's first used.
        return;
    }
    if (provider.getClass().equals(BouncyCastleProvider.class)) {
        // BC with same package name, shouldn't happen in real life.
        return;
    }
    // Android registers its own BC provider. As it might be outdated and might not include
    // all needed ciphers, we substitute it with a known BC bundled in the app.
    // Android's BC has its package rewritten to "com.android.org.bouncycastle" and because
    // of that it's possible to have another BC implementation loaded in VM.
    Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
    Security.insertProviderAt(new BouncyCastleProvider(), 1);
}

Discussion about this issue is here https://github.com/web3j/web3j/issues/828

Joe Dow
  • 583
  • 1
  • 3
  • 12