我有一个为我创建证书的外部服务,我收到了一个缓冲区(字符串)。我尝试将此缓冲区加载到Java中的KeyStore中,然后使用“store”函数创建.p12文件。但是,存储函数抛出异常 - “给定最终块未正确填充”。
无论我尝试什么,我都无法解决这个问题。
我的代码是:
public void createP12Certificate(String userName, String comment) throws KeyStoreException, AdminCertificateException, CertificateException, NoSuchAlgorithmException, IOException
{
KeyStore store = KeyStore.getInstance("PKCS12");
/* Some Code that gets 'buff' etc. */
byte[] byteBuff = hexStringToByteArray(buff);
Arrays.reverse(byteBuff);
InputStream inputStream = new ByteArrayInputStream(byteBuff);
store.load(inputStream, password.toCharArray());
OutputStream outputStream = new FileOutputStream(userName+".p12");
store.store(outputStream,anotherPassword); //Throws Exception
}
非常感谢!
答案 0 :(得分:1)
问题在于那些
/* Some Code that gets 'buff' etc. */
byte[] byteBuff = hexStringToByteArray(buff);
因为其他发布的代码可以正常运行。
char[] passwordChars = "password".toCharArray();
String fileOne = "/tmp/output_1.p12";
String fileTwo = "/tmp/output_2.p12";
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(null, null);
keyStore.store(new FileOutputStream(fileOne), passwordChars);
keyStore = KeyStore.getInstance("PKCS12");
byte[] byteBuff = Files.readAllBytes(Paths.get(fileOne));
InputStream inputStream = new ByteArrayInputStream(byteBuff);
keyStore.load(inputStream, passwordChars);
keyStore.store(new FileOutputStream(fileTwo), passwordChars);