简体   繁体   English

使用密钥库创建.p12文件

[英]Creating a .p12 file with a KeyStore

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. 我尝试将此缓冲区加载到Java中的KeyStore中,然后使用“存储”功能来创建.p12文件。 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! 非常感谢你!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM