开发者

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 bou开发者_Python百科ncy 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?


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜