本文介绍了没有提供BouncyCastle的加密算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用BouncyCastle的与Android实现ECDH和EL贾迈勒。我已经添加了BouncyCastle的jar文件(bcprov-jdk16-144.jar),并写了一些code,与我的电脑JVM工作但是当我试着将它移植到我的Andr​​oid应用程序,它抛出:

  java.security.NoSuchAlgorithmException:KeyPairGenerator的ECDH实现未发现

在code的样本是:

  Security.addProvider(新org.bouncycastle.jce.provider.BouncyCastleProvider());java.security.KeyPairGenerator凯基= org.bouncycastle.jce.provider.asymmetric.ec.KeyPairGenerator.getInstance(ECDH,BC);
                ECGenParameterSpec ecSpec =新ECGenParameterSpec(prime192v1);                keyGen.initialize(ecSpec,SecureRandom.getInstance(SHA1PRNG));                密钥对对= keyGen.generateKeyPair();
                公钥pubk = pair.getPublic();
                PrivateKey prik = pair.getPrivate();

我然后写一个简单的程序,看看有什么加密算法都可以跑在我的Andr​​oid模拟器和我的电脑JVM中的code是:

 设置< Provider.Service> RAR =新org.bouncycastle.jce.provider.BouncyCastleProvider()getServices()。
    迭代器< Provider.Service> IR = rar.iterator();
    而(ir.hasNext())
        的System.out.println(ir.next()信息getAlgorithm());

在Android上我没有得到任何欧共体算法,而常在我的电脑上运行它的罚款。

我编译了很多的充气城堡类的时候也越来越以下两个错误

17 01-07:17:42.548:信息/ dalvikvm(1054):DexOpt:不解决暧昧类的Lorg / BouncyCastle的/ ASN 1 / ASN1Encodable;

17 01-07:17:42.548:DEBUG / dalvikvm(1054):DexOpt:未验证Lorg / BouncyCastle的/ ASN 1 / ESS / OtherSigningCertificate;':多重定义

我在做什么错了?


解决方案

您可能想 - 一个重新包装我做充气城堡的专门针对为Android。这里注意的是:

<一个href=\"http://$c$c.google.com/p/android/issues/detail?id=3280\">http://$c$c.google.com/p/android/issues/detail?id=3280

... Android平台集成不幸充气城堡,这也使得由于类加载器冲突的安装难度库的更新版本的简化版本 - 即使你添加完整的BC罐子,你没有得到您添加的其他类。

海绵城堡是一个充满更换为充气城堡加密库船它与Android的版本残废。有一对夫妇的小的变化,使其在Android上工作:


  • 所有程序包名称已经从org.bouncycastle移动*到org.spongycastle * - 。所以没有冲突的类加载器

  • Java安全API提供的名字是现在的 SC 而非 BC

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?

解决方案

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

这篇关于没有提供BouncyCastle的加密算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 01:48