Creating a .p12 file with a KeyStore

岁酱吖の 提交于 2019-12-08 11:46:24

问题


I have an external service that creates certificate for me, out of which I recieve a buffer (String). I attempt to load this buffer into a KeyStore in Java and then use the "store" function in order to create a .p12 file. However, the store function throws an exception - "Given final block not properly padded".

No matter what I try, I cannot get this to work or find the cause of the issue.

My code is :

    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
}

Thank you very much!


回答1:


The issue is at those lines

/* Some Code that gets 'buff' etc. */
byte[] byteBuff = hexStringToByteArray(buff);

Because the other posted code would work without an exception.

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);


来源:https://stackoverflow.com/questions/37566426/creating-a-p12-file-with-a-keystore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!