如何将 bouncycastle 算法添加到 android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2584401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add bouncycastle algorithm to android
提问by Vamsi
I am trying to write a small application using bouncycastle algorithm, from the BouncyCastleProvider.javait says we have to import and add the provider during runtime by the following code
我正在尝试使用bouncycastle算法编写一个小应用程序,从BouncyCastleProvider.java它说我们必须在运行时通过以下代码导入和添加提供程序
import org.bouncycastle.jce.provider.BouncyCastleProvider;
Security.addProvider(new BouncyCastleProvider());
import org.bouncycastle.jce.provider.BouncyCastleProvider;
Security.addProvider(new BouncyCastleProvider());
error - The import org.bouncycastle cannot be resolved; during import error - BouncyCastleProvider cannot be resolved to a type; when calling addProvider
错误 - 无法解析导入 org.bouncycastle;导入错误期间 - BouncyCastleProvider 无法解析为类型;调用 addProvider 时
I though bouncycastle is not provided with the Android 1.6 SDK, so thought of installing separately. how should i do this? If Bouncycastle is shipped along with SDK, what should i do to avoid these errors? I am using Android 1.6, eclipse-V3.4.0 on winXP . Thanks in advance
我虽然 Android 1.6 SDK 没有提供 bouncycastle,所以想单独安装。我该怎么做?如果 Bouncycastle 与 SDK 一起提供,我应该怎么做才能避免这些错误?我在 winXP 上使用 Android 1.6,eclipse-V3.4.0。提前致谢
采纳答案by Jim Blackler
I'm not familiar with this particular library. However, here are general instructions on how to include a library delivered as a 'jar' file into an Android project.
我不熟悉这个特定的图书馆。但是,这里是关于如何将作为“jar”文件交付的库包含到 Android 项目中的一般说明。
Download the jar file and put it somewhere on your workstation. You may want to put in the root directory of the project your are installing it in, or maybe in a 'lib' directory in the root.
下载 jar 文件并将其放在工作站上的某个位置。您可能希望将其放入要安装它的项目的根目录中,或者放入根目录中的“lib”目录中。
In Eclipse, select Project->Properties, then select Java Build Path. Then click Add External Jars, navigate to where you put the .jar file, select it and click Open.
在 Eclipse 中,选择 Project->Properties,然后选择 Java Build Path。然后单击 Add External Jars,导航到放置 .jar 文件的位置,选择它并单击 Open。
Now type or paste some code that attempts to use the classes in the jar. If you are lucky a light bulb icon will appear in the left margin. Clicking on this will prompt you to add the correct Import statement to the top of your .java file.
现在键入或粘贴一些尝试使用 jar 中的类的代码。如果幸运的话,左边缘会出现一个灯泡图标。单击此按钮将提示您将正确的 Import 语句添加到 .java 文件的顶部。
There are still things that can go wrong at this point. The library may make use of java.* or javax.* content not supplied by Android (it has just a subset of these libraries). Also it may have additional library dependencies of its own. There are other reasons why the .jar may not be compatible with the Android platform.
在这一点上,仍有一些事情可能会出错。该库可能会使用 Android 未提供的 java.* 或 javax.* 内容(它只有这些库的一个子集)。它也可能有自己的附加库依赖项。.jar 可能与 Android 平台不兼容还有其他原因。
Note also that it will increase the size of your .apk to accommodate the new content.
另请注意,它会增加 .apk 的大小以容纳新内容。
回答by Carl Whalley
Or better still use SpongyCastlesince the BC shipped with Android is both crippled and old.
或者最好还是使用SpongyCastle,因为 Android 附带的 BC 既残废又旧。
回答by Adrian
You shouldn't need to explicitly add BouncyCastle as a provider. As you say, it's already included with Android.
您不需要显式添加 BouncyCastle 作为提供程序。正如你所说,它已经包含在 Android 中。
Here's what I do to get a BouncyCastle AES cipher,
这是我为获得 BouncyCastle AES 密码所做的工作,
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithSHA256And256BitAES-CBC-BC");
If you look in BouncyCastleProvider.javayou'll see a reference to PBEWithSHA256And256BitAES-CBC-BC along with a few other ciphers provided by BouncyCastle.
如果您查看BouncyCastleProvider.java,您将看到对 PBEWithSHA256And256BitAES-CBC-BC 的引用以及 BouncyCastle 提供的其他一些密码。
回答by wolfkabal
You would need to compile the BC library under a different name, as it will have a conflict with the built-in BC already in Android - recompile and reference as BC2 or org.BouncyCastle2.x
您需要以不同的名称编译 BC 库,因为它会与 Android 中已有的内置 BC 发生冲突 - 重新编译并引用为 BC2 或 org.BouncyCastle2.x