Bouncycastle encryption algorithms not provided

I'm trying to use BouncyCastle with android to implement ECDH and EL Gamal. I've added the bouncycastle jar file (bcprov-jdk16-144.jar) and written some code that works with my computers jvm however when I try and port it to my android application it throws:

java.security.NoSuchAlgorithmException: KeyPairGenerator ECDH implementation not found

A sample of the code is:

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

java.security.KeyPairGenerator keyGen = org.bouncycastle.jce.provider.asymmetric.ec.KeyPairGenerator.getInstance("ECDH", "BC");
                ECGenParameterSpec ecSpec = new ECGenParameterSpec("prime192v1");

                keyGen.initialize(ecSpec, SecureRandom.getInstance("SHA1PRNG"));



                KeyPair pair = keyGen.generateKeyPair();
                PublicKey pubk = pair.getPublic();
                PrivateKey prik = pair.getPrivate();

I then wrote a simple program to see what encryption algorithms are available and ran it on my android emulator and on my computers jvm the code was:

Set<Provider.Service> rar = new org.bouncycastle.jce.provider.BouncyCastleProvider().getServices();
    Iterator<Provider.Service> ir = rar.iterator();
    while(ir.hasNext())
        System.out.println(ir.next().getAlgorithm());

On android I do not get any of the EC algorithms while ran normally on my computer it's fine.

I'm also getting the following two errors when compiling for a lot of the bouncy castle classes:

01-07 17:17:42.548: INFO/dalvikvm(1054): DexOpt: not resolving ambiguous class 'Lorg/bouncycastle/asn1/ASN1Encodable;'

01-07 17:17:42.548: DEBUG/dalvikvm(1054): DexOpt: not verifying 'Lorg/bouncycastle/asn1/ess/OtherSigningCertificate;': multiple definitions

What am I doing wrong?


Asked by: Stella569 | Posted: 25-01-2022






Answer 1

You probably want Spongy Castle - a repackage I made of Bouncy Castle specifically targeted for Android. As noted here:

http://code.google.com/p/android/issues/detail?id=3280

...the Android platform unfortunately incorporates a cut-down version of Bouncy Castle, which also makes installing an updated version of the libraries difficult due to classloader conflicts - even when you add your full BC jar, you don't get the additional classes you added.

Spongy Castle is a full replacement for the crippled versions of the Bouncy Castle cryptographic libraries which ship with Android. There are a couple of small changes to make it work on Android:

  • all package names have been moved from org.bouncycastle.* to org.spongycastle.* - so no classloader conflicts
  • the Java Security API Provider name is now SC rather than BC

Answered by: Andrew412 | Posted: 26-02-2022



Answer 2

I don't know if this answers your question, but some of the BouncyCastle libraries are already in the Android SDK. Perhaps the error about ambiguous class is because BouncyCastle is already included in the emulator.

It seems you can use it via the javax.crypto.Cipher class.

Answered by: Carlos671 | Posted: 26-02-2022



Answer 3

If you browse the Android code you will see not all BouncyCastle functionalities are included. see libcore/security/src/main/java/org/bouncycastle/jce/provider/BouncyCastleProvider.java

In particular the output of ECDH is commented out, which means at compilation it will be completely left out from the android jar file.

Answered by: Samantha683 | Posted: 26-02-2022



Similar questions

encryption - BouncyCastle on Android

I am using PBEWITHSHA256AND256BITAES-CBC-BC to encrypt binary data which contains sensitive user data I just want to confirm if BouncyCastle is part of every Android SDK after (2.1) as I read a on a few sites that BouncyCastle is not part of every Android SDK. Would like a definitive answer


java - Bouncycastle elliptic curve encryption on Android

I'm trying to decrypt an X509 cert on an android device using Bouncycastle. However, I am consistently getting java.lang.RuntimeException: algorithm identifier 1.2.840.10045.2.1 in key not recognised. However, when I run the exact same code on my own computer, it works perfectly fine. Is android overriding some of the bouncycastle libraries with an outdated version?


android - OpenSSL Encryption not Compatible with BouncyCastle Decryption?

I am sending a encrypted mail using OpenSSL in an android app. While sending the mail this is the relevant native code - cipher = EVP_rc2_40_cbc(); int flags = PKCS7_STREAM; PKCS7* p7 = PKCS7_encrypt(certs, bio, cipher, flags); int r = SMIME_write_PKCS7(out, p7, bio, flags); The mail is successfully sent to the server and I get a sent mail notification. Now when I am trying to o...


How do I RSA encrypt a string with a plaintext key using Java BouncyCastle API on Android

I am trying to encrypt a string using the BouncyCastle API in Android to send off to a server. I have the public key in plaintext (in memory, not in the filesystem, of course! no need to yell at me, cryptographers ;) ) and I need to use this plaintext public key to encrypt a string to an RSA encrypted string. This is my class: public class RSAEncryptor { //Get certificate from base64 string ...


bouncycastle - Unlimited Strength Jce and Android

I'm using SpongyCastle (full implementation of BouncyCastle's crypto functions for Android) and I have a bks that contains a key of size 384. I'm trying to extract that key using the method KeyStore.getKey(alias, password) as you would any key in a keystore. But what I'm running into is the error java.security.UnrecoverableKeyException: no match. Doing a little bit of research indicates that i...


encryption - BouncyCastle on Android

I am using PBEWITHSHA256AND256BITAES-CBC-BC to encrypt binary data which contains sensitive user data I just want to confirm if BouncyCastle is part of every Android SDK after (2.1) as I read a on a few sites that BouncyCastle is not part of every Android SDK. Would like a definitive answer


certificate - How to read Cipher private Key with bouncycastle in android?

I have a private key that encrypted with AES256 and key salam. I want to decrypt it and read the private key in that. I Write Below Code : public CertReader(String password, Context context) { mContext = context; pf = new Password(password.toCharArray()); InputStream Certin = mContext.getResources().openRawResource( R.raw.kxx_...


How to handle BouncyCastle change and test Android system upgrade?

My application is dependent on BouncyCastle library. When system is upgraded to 2.3, version of this library is also upgraded. So when user upgrades system all of data that was encrypted can not be decrypted anymore. First question - can I somehow avoid this situation? Be able to decrypt even after upgrade. Second - How to simulate system upgrade to check if my code that says "sorry for your data loss" work...


bouncycastle - Bouncy Castle key generation extremely slow on Android

I have an Android crypto app that's built with Bouncy Castle (actually using Spongy Castle but essentially the same thing). For some reason key generation is EXTREMELY slow (on the order of several minutes). Here is a test method I'm using: @Test public void testHMAC() { System.out.println(getTestAnnouncement("testHMAC")); long start = System.currentTimeMillis(); final PBEParametersGen...


java - Does BouncyCastle have a SecureRandom service?

I'm trying to generate cryptographically secure random numbers using Java and using the following code section to create a SecureRandom object to view its provider and algorithm: Provider prov=new org.spongycastle.jce.provider.BouncyCastleProvider(); Security.insertProviderAt(prov, 1); SecureRandom sr=new SecureRandom(); srProvider=sr.getProvider().toString(); srAlgorithm=sr.getAlgorithm(); ...


java - Bouncycastle elliptic curve encryption on Android

I'm trying to decrypt an X509 cert on an android device using Bouncycastle. However, I am consistently getting java.lang.RuntimeException: algorithm identifier 1.2.840.10045.2.1 in key not recognised. However, when I run the exact same code on my own computer, it works perfectly fine. Is android overriding some of the bouncycastle libraries with an outdated version?


java - Encrypt mail with SMIME and X.509 using BouncyCastle

I'm trying to send a encrypted mail with JavaMail Library and BouncyCastle Library: This is the code I've written, or I followed a tutorial: public class SendMail extends javax.mail.Authenticator { private String _user; private String _pass; private String[] _to; private String _from; private String _port; private String _sport; private String _host; private String _su...


Is BouncyCastle provided in Android prior to Gingerbread?

If so, in what versions is it available? I only see it go as far back as Gingerbread here: https://android.googlesource.com/platform/external/bouncycastle/ Was it packaged under a different repository prior to GB?






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top