Search code examples
androidrsabouncycastle

Why do I get NoSuchAlgorithmException in android when trying to encrypt with bouncycastle?


First of all note that all the code is already tested on my linux machine. There are relevant unit tests to confirm it. But the code does not work on android.

I have included my own bouncycastle v1.48 library in my project. I am not sure if android has this library already embedded and I hope this does not bring any conflicts(?)

I am using as a public key algorithm tag the RSA_GENERAL and this is useful when creating the secret key like that:

new PGPSecretKey(
  PGPSignature.DEFAULT_CERTIFICATION,
  publicKeyAlgorithmTag,
  publicKey,
  privateKey,
  new Date,
  identity.toString,
  symmetricKeyAlgorithmTag,
  passPhrase,
  null,
  null,
  new SecureRandom(),
  BC_PROVIDER_NAME
);

But when creating the key pair I am not using this tag because the creating happens from java.security, not from bouncycastle. This is a portion of the relevant code (in Scala):

val generator = KeyPairGenerator.getInstance(PUBLIC_KEY_ALGORITHM_STRING, BC_PROVIDER_NAME);
generator.initialize(KEY_SIZE_IN_BITS, new SecureRandom());
generator.generateKeyPair();

The public key algorithm string that you see above is simply the string "RSA"

Note: I already had a NoSuchAlgorithmException with the symmetric key algorithm but I overcomed it easily by switching from CAST5 to BLOWFISH.

Do you have any suggestions on where I might start searching for solutions or what is the issue? Thank you


Solution

  • I have included my own bouncycastle v1.48 library in my project.

    That will not work. Android has its own trimmed-down version of Bouncy Castle (mostly in support of javax.crypto), and Android will use its library, not yours. Try switching to spongycastle instead. Here is a StackOverflow answer on spongycastle written by the library's author.

    As is described in the spongycastle documentation, you must change all imports from org.bouncycastle.* to org.spongycastle.*. Also, any place where you need to provide a provider name, you need to change that from "BC" to "SC".