简体   繁体   English

BouncyCastle安装问题

[英]BouncyCastle installation problems

I'm trying to add BouncyCastle as a security provider on Windows XP Pro so I can use it to add some certs to an Android application per the instructions here . 我正在尝试将BouncyCastle添加为Windows XP Pro上的安全提供程序,因此我可以使用它根据此处的说明向Android应用程序添加一些证书。 Unfortunately I can't get it to add the provider. 不幸的是我不能让它添加提供者。

I've: 我有:

  1. Downloaded the provider to C:\\Program Files\\Java\\jre6\\lib\\ext\\ . 将提供程序下载到C:\\Program Files\\Java\\jre6\\lib\\ext\\
  2. Added C:\\Program Files\\Java\\jre6\\lib\\ext\\bcprov-jdk16-146.jar to %CLASSPATH% . C:\\Program Files\\Java\\jre6\\lib\\ext\\bcprov-jdk16-146.jar%CLASSPATH%
  3. Added security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider to java.security (7 being the next int in the order). security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider添加到java.security(7是顺序中的下一个int)。

When I run: 当我跑:

keytool -import -v -trustcacerts -alias 0 -file mycert.crt -keystore mystore.bks -storetype BKS -providerName org.bouncycastle.jce.provider.BouncyCastleProvider -storepass mypassword 

I get the following error message: 我收到以下错误消息:

keytool error: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider

I've also tried adding it dynamically: 我也试过动态添加它:

import java.security.Provider;
import java.security.Security;
import java.util.Enumeration;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class BouncyCastleMain {

    public static void main(String[] args) throws Exception {
        Security.addProvider(new BouncyCastleProvider()); // add it
        try { // list them out
            Provider p[] = Security.getProviders();
            for (int i = 0; i < p.length; i++) {
                System.out.println(p[i]);
                for (Enumeration<?> e = p[i].keys(); e.hasMoreElements();)
                    System.out.println("\t" + e.nextElement());
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

At first I got an access error when compiling the java class, but changed it to a warning per the suggestion here . 起初我在编译java类时遇到了访问错误,但是根据这里的建议将其更改为警告。 Now when I run the code it shows BouncyCastle in the list of providers but it doesn't stick around after the program is done. 现在,当我运行代码时,它会在提供程序列表中显示BouncyCastle,但在程序完成后它不会停留。

I'm sure it must be doable, but I'm stymied over how to get this guy installed long enough to run keytool using it. 我敢肯定它一定是可行的,但我对如何让这个人安装足够长时间来运行keytool使用它感到困扰。 Is it possible to run keytool via a java API, or could there be some step I've missed that will make the provider stick around? 是否可以通过Java API运行keytool,或者是否有一些我错过的步骤会让提供商坚持下去?

Thanks! 谢谢!

The -providerName option requires a provider name ("BC", in this case), not a class name . -providerName选项需要提供程序名称 (在本例中为“BC”),而不是类名 An alternative option, -providerClass , does require a class name, and it is useful when the provider isn't registered in the java.security file. 另一个选项-providerClass确实需要一个类名,当提供程序未在java.security文件中注册时,它很有用。

When you register a provider "programatically", it is only temporary. 当您以“编程方式”注册提供程序时,它只是暂时的。 Your program must re-register its provider each time it runs. 您的程序必须在每次运行时重新注册其提供程序。 You won't be able to use this approach if your goal is to make BouncyCastle available to keytool . 如果您的目标是使BouncyCastle可用于keytool您将无法使用此方法。

Since you've already installed the provider (by putting the archive in lib/ext and listing it in java.security ), using the -providerName BC option is probably the easiest solution. 由于您已经安装了提供程序(通过将存档放在lib/ext并将其列在java.security ),因此使用-providerName BC选项可能是最简单的解决方案。 Alternatively, you can use the -providerClass org.bouncycastle.jce.provider.BouncyCastleProvider option. 或者,您可以使用-providerClass org.bouncycastle.jce.provider.BouncyCastleProvider选项。

By the way, you should not use the CLASSPATH environment variable. 顺便说一句,您不应该使用CLASSPATH环境变量。 Libraries in lib/ext are on the class path already. lib/ext中的库已经在类路径上了。

If, after correcting the options, you still get a NoSuchProviderException (using -providerName ) or ClassNotFoundException (using -providerClass ), verify that you are using the right copy of keytool . 如果在更正选项后仍然得到NoSuchProviderException (使用-providerName )或ClassNotFoundException (使用-providerClass ),请验证您使用的是正确的keytool副本。 That is, when executing, specify the full path of keytool , rather than relying on your PATH variable. 也就是说,在执行时,指定keytool的完整路径,而不是依赖于PATH变量。 Make sure that the path refers to the JRE into which BouncyCastle was installed. 确保路径引用安装了BouncyCastle的JRE。 It isn't uncommon for a system to have multiple JREs and JDKs. 系统具有多个JRE和JDK的情况并不少见。

如果您使用的是Windows,请不要忘记以管理员身份启动命令行以输入keytool命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM