我需要将一个可信证书导入到一个已经存在的密钥库中,这是我的代码,但它给我一个EOFException,可能是什么错误?
public void importTrustedCertificate( String alias, byte [] trustedCertificate )
throws Exception
{
KeyStore keyStore = KeyStore.getInstance( "JKS" );
FileInputStream fileInputStream = new FileInputStream( "keystore" + File.separator + "ClientRegistrarKeyStore.jks" );
FileOutputStream fileOutputStream = new FileOutputStream( "keystore" + File.separator + "ClientRegistrarKeyStore.jks" );
keyStore.load( fileInputStream, "keystore".toCharArray() );
keyStore.setCertificateEntry( alias, new X509Certificate( trustedCertificate ) );
keyStore.store( fileOutputStream, "keystore".toCharArray() );
fileInputStream.close();
fileOutputStream.close();
return;
}
错误:
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:375)
at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:628)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:38)
at java.security.KeyStore.load(KeyStore.java:1185)
at com.netrust.passportverification.clientregistrar.setup.IniFileGenerator.importTrustedCertificate(IniFileGenerator.java:107)
at com.netrust.passportverification.clientregistrar.setup.IniFileGenerator.processZipFile(IniFileGenerator.java:165)
at com.netrust.passportverification.clientregistrar.setup.IniFileGenerator.main(IniFileGenerator.java:180)
Java Result: 1
答案 0 :(得分:4)
您确定此位置的文件不为空吗?可以keytool
列出其内容吗?这个EOFException
看起来并不特定于密钥库,但是您尝试加载的初始文件似乎比它应该更短。
此外,您的FileInputStream
和FileOutputStream
引用了同一个文件。我建议在写入另一个之前关闭你读的那个,以避免冲突:
FileInputStream fileInputStream = new FileInputStream( "keystore" + File.separator + "ClientRegistrarKeyStore.jks" );
keyStore.load( fileInputStream, "keystore".toCharArray() );
fileInputStream.close();
keyStore.setCertificateEntry( alias, new X509Certificate( trustedCertificate ) );
FileOutputStream fileOutputStream = new FileOutputStream( "keystore" + File.separator + "ClientRegistrarKeyStore.jks" );
keyStore.store( fileOutputStream, "keystore".toCharArray() );
fileOutputStream.close();
答案 1 :(得分:0)
试试这个...
证书证书= keyStore.getCertificate(别名);
keyStore.setCertificateEntry(别名,证书);