我用pkcs12格式存储密钥,但是出现一个很奇怪的问题:我为KeyEntry设和keyStore设置了不同的口令(分别是PRIV_PASSWORD KEYSTORE_PASSWORD)读取时keyStore时总是出现口令错误。后来发现读取时,getKey和load时都要用KEYSTORE_PASSWORD就好了。但是这显然跟jdk里说的是不相符合的。而且用bks格式的时候,getKey用PRIV_PASSWORD才正确!
下面是部分代码:
实在不知道问题出在哪里了,忘高手执教!
public void writeKey(){
//.....
KeyStore keyStore_All = KeyStore.getInstance("PKCS12","BC");
//.......
keyStore_All.setKeyEntry(this.USERKEY + 0, this.privKey, PRIV_PASSWORD, chain);
FileOutputStream fos = new FileOutputStream(FILENAME);
keyStore_All.store(fos, KEYSTORE_PASSWORD);
//........
}
public JCEECPrivateKey readKey(){
KeyStore ks = KeyStore.getInstance("PKCS12",BC");
FileInputStream fis = new FileInputStream(FILENAME);
ks.load(fis, this.KEYSTORE_PASSWORD);
userPrivateKey = (JCEECPrivateKey) ks.getKey(this.USERKEY + 0, PRIV_PASSWORD);
//此处用KEYSTORE_PASSWORD才能正常读取!但是jdk里说明是要
//用PRIV_PASSWORD的啊
//userPrivateKey = (JCEECPrivateKey) ks.getKey(this.USERKEY + 0, KEYSTORE_PASSWORD);
fis.close();
}