使用密钥库创建.p12文件 [英] Creating a .p12 file with a KeyStore

查看:97
本文介绍了使用密钥库创建.p12文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为我创建证书的外部服务,我从中获得一个缓冲区(字符串)。我尝试将此缓冲区加载到Java中的KeyStore中,然后使用存储功能来创建.p12文件。但是,存储功能会引发异常-给最终块未正确填充。

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.

我的代码是:

    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
}

非常感谢!

推荐答案

问题出在那几行

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

这篇关于使用密钥库创建.p12文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆