我有一个密钥库,其中包含由以下命令生成的密钥对:
keytool -genkeypair -v -alias test-agent -keypass test-agent -storepass 123456.ABC -keystore test-agent.keystore -storetype JKS
我填写了证书的请求信息,并正确生成了带有密钥对的存储。
以下命令:
keytool -list -keystore test-agent.keystore -storepass 123456.ABC -storetype JKS
回报:
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 1 entry
test-agent, Jul 13, 2012, PrivateKeyEntry,
Certificate fingerprint (MD5): 7B:8F:D7:25:FF:34:D0:EF:44:87:46:E5:BF:18:C6:BF
现在我将密钥库文件添加到我的构建路径并尝试使用在 OSX Lion 上运行的以下 java 代码加载它:
public void loadKeyStore() {
try {
final Provider p = Security.getProvider("SUN");
final KeyStore keystore = KeyStore.getInstance("JKS",p);
final InputStream keyStoreInStream = this.getClass().getClassLoader().getResourceAsStream("test-agent.keystore");
if ( keyStoreInStream == null ) throw new RuntimeException("No keystore found!");
final char[] password = "123456.ABC".toCharArray();
try {
keystore.load(keyStoreInStream, password);
} catch (Exception e) {
log.error(String.format("Security library error! [%s]",e.getCause()),e);
}
} catch (KeyStoreException e) {
log.error("Can't initialize security library!",e);
}
}
抛出以下异常:
java.io.IOException: Invalid keystore format
at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:633)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:38)
at java.security.KeyStore.load(KeyStore.java:1185)
我已经尝试过使用 PKCS12(为 keytool 和代码设置,在这种情况下,提供者分别应该是 SunJSSE),这会导致另一个异常:
java.io.IOException: DerInputStream.getLength(): lengthTag=111, too big.
at sun.security.util.DerInputStream.getLength(DerInputStream.java:544)
at sun.security.util.DerValue.init(DerValue.java:347)
at sun.security.util.DerValue.<init>(DerValue.java:303)
at com.sun.net.ssl.internal.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1200)
at java.security.KeyStore.load(KeyStore.java:1185)
我不知道问题是什么。任何人都可以给我一个提示吗?