5

I'm trying to use KeyStore in order to get info from a keystore. I've generated the keystore using this command:

keytool -genkey -alias server -keyalg RSA -keystore server.keystore -validity 365 taken this page.

Checking its info keytool -list -v -keystore server.keystore I get the following:

Alias name: server
Creation date: Apr 30, 2014
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
(other info here)

Using this command: keytool -list -keystore server.keystore -alias server I get this:

server, Apr 30, 2014, PrivateKeyEntry, Certificate fingerprint (SHA1): 28:65:5B:0C:B3:3C:C9:AA:F1:7C:CE:91:23:77:DD:0D:F8:54:70:B9

Now, my java code:

keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(getClass().getResourceAsStream(KEYSTORE_FILE_PATH), "myPass".toCharArray());
keyStore.getCertificate("server").getPublicKey().getEncoded(); //here I get a null pointer exception - keystore.getCertificate("server") returns null. 

Doing keyStore.aliases() returns an EmptyEnumeration.

The application uses maven, java ee 7 and I've copied the keystore file in the resources folder of my application. KEYSTORE_FILE_PATH has the value of "/server.keystore".

Thanks.

4

1 回答 1

6

Class.getResourceAsStream()null在没有指定名称的资源时 返回。KeyStore.load()null在传递输入流时将密钥库重置为空状态。

这意味着在运行时您的代码找不到密钥库资源并默默地继续使用空的密钥库。

  • 添加一个保护条件,getResourceAsStream()在将值传递给KeyStore.load().
  • 在 maven 中检查您的代码和构建/打包过程,以确保密钥库文件存在于正确的位置。

一些 关于这方面的问题 可能对您有所帮助。 getResourceAsStream()

于 2014-04-30T20:50:22.000 回答