未提供 Bouncycastle 加密算法
我正在尝试将 BouncyCastle 与 android 一起使用来实现 ECDH 和 EL Gamal。我添加了 bouncycastle jar 文件(bcprov-jdk16-144.jar)并编写了一些适用于我的计算机 jvm 的代码,但是当我尝试将其移植到我的 android 应用程序时,它会抛出:
java.security.NoSuchAlgorithmException: KeyPairGenerator ECDH implementation not found
示例是:
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();
代码 编写了一个简单的程序来查看可用的加密算法,并在我的 android 模拟器和我的计算机 jvm 上运行它,代码是:
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());
在 android 上,我没有得到任何 EC 算法,而在我的计算机上正常运行则很好。
在编译很多充气城堡类时,我还遇到以下两个错误:
01-07 17:17:42.548: INFO/dalvikvm(1054): DexOpt: not resolving ambigeous class 'Lorg/bouncycastle/asn1/ASN1Encodable ;'
01-07 17:17:42.548:DEBUG / dalvikvm(1054):DexOpt:不验证'Lorg / bouncycastle / asn1 / ess / OtherSigningCertificate;':多个定义
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想要 Spongy Castle - 我用 Bouncy Castle 专门针对 Android 制作的重新包装。如此处所述:
http://code.google.com/p/android /issues/detail?id=3280
...不幸的是,Android 平台包含了 Bouncy Castle 的精简版本,这也使得由于类加载器冲突而导致安装库的更新版本变得困难 - 即使您添加了完整的 BC jar,您不会获得添加的附加类。
Spongy Castle 完全替代了 Android 附带的 Bouncy Castle 加密库的残缺版本。为了使其在 Android 上运行,需要进行一些小更改:
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:
我不知道这是否能回答您的问题,但一些 BouncyCastle 库已经包含在 Android SDK 中。也许有关模糊类的错误是因为 BouncyCastle 已包含在模拟器中。
看来您可以通过 javax.crypto.Cipher 类使用它。
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.
如果您浏览 Android 代码,您会发现并未包含所有 BouncyCastle 功能。
请参阅 libcore/security/src/main/java/org/bouncycastle/jce/provider/BouncyCastleProvider.java
特别是 ECDH 的输出被注释掉,这意味着在编译时它将完全从 android jar 文件中删除。
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.