无法从文件中实例化JavaKeyStore

时间:2013-04-26 16:03:24

标签: java security cryptography keystore jks

我正在尝试使用org.apache.ws.security.components.crypto.Merlin专门使用org.apache.ws.security.components.crypto.CryptoFactory方法获取CryptoFactory.getInstance(properties)的实例。

这将一直抛出

java.lang.RuntimeException: org.apache.ws.security.components.crypto.Merlin cannot create instance

这显然是由

引起的

java.security.UnrecoverableKeyException: Password verification failed

使用命令行上的keytool检查密钥库文件上的密码是否正确。

通过以下过程生成密钥库:

CMD Window detailing keytool -genkey -keystore testkeystore.jks

位于eclipse项目的根目录中。

测试应用如下:

public class App {
    public static void main(String[] args) throws CredentialException,
            IOException {
        System.out.println("Starting");
        Properties p = new Properties();
        p.setProperty("org.apache.ws.security.crypto.merlin.keystore.password",
                "password");
        p.setProperty("org.apache.ws.security.crypto.provider",
                "org.apache.ws.security.components.crypto.Merlin");
        p.setProperty("org.apache.ws.security.crypto.merlin.keystore.type",
                "jks");
        p.setProperty("org.apache.ws.security.crypto.merlin.file", "./testkeystore.jks");

        Crypto crypto = CryptoFactory.getInstance(p);
        System.out.println(" Complete ");

    }
}

并生成以下异常:

Exception in thread "main" java.lang.RuntimeException: org.apache.ws.security.components.crypto.Merlin cannot create instance
    at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:225)
    at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:180)
    at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:73)
    at com.restart.test.cryptotest2.App.main(App.java:22)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.apache.ws.security.components.crypto.CryptoFactory.loadClass(CryptoFactory.java:211)
    ... 3 more
Caused by: org.apache.ws.security.components.crypto.CredentialException: Failed to load credentials.
    at org.apache.ws.security.components.crypto.AbstractCrypto.load(AbstractCrypto.java:174)
    at org.apache.ws.security.components.crypto.AbstractCrypto.<init>(AbstractCrypto.java:135)
    at org.apache.ws.security.components.crypto.Merlin.<init>(Merlin.java:71)
    ... 8 more
Caused by: java.io.IOException: Keystore was tampered with, or password was incorrect
    at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:772)
    at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:55)
    at java.security.KeyStore.load(KeyStore.java:1214)
    at org.apache.ws.security.components.crypto.AbstractCrypto.load(AbstractCrypto.java:168)
    ... 10 more
Caused by: java.security.UnrecoverableKeyException: Password verification failed
    at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:770)
    ... 13 more

cmd窗口中指示的密码设置为“password”,但是应用程序拒绝了所有帐户,我可以使用keytool -storepasswd更改密码而没有任何问题,所以我知道密码我我提供的是正确的;任何人都可以建议这里可能出错的地方?我一直试图调试这个失败了一整天。

如果我能提供任何其他信息,请告诉我。

编辑 -

构建此测试需要以下maven依赖项:

   <dependency> 
      <groupId>org.apache.ws.security</groupId> 
      <artifactId>wss4j</artifactId> 
      <version>1.5.8</version> 
      <scope>provided</scope>
    </dependency>

2 个答案:

答案 0 :(得分:3)

在阅读了用户的评论后,我针对不同版本的JDK / JRE构建了它,并且在下载了rt.java的源代码并且单步执行后,我发现CryptoBase类实例化了两个JavaKeyStores,第一个(是我的.jks文件),它实例化得很好,但第二个是cacerts中的jre\lib\security>密钥库,它没有导致失败的默认密码changeit;

我现在更改了jre密钥库上的密码,我在原来的jre / jdk中工作正常。

答案 1 :(得分:1)

我认为您的问题与库有关,因为stacktrace说

'org.apache.ws.security.components.crypto.Merlin无法创建实例'

这意味着您没有(o版本不正确)的WSS4J库。