use of com.itrus.cert.X509Certificate in project spring-cloud-digital-sign by SpringForAll.
the class ServerPKCSUtil method genP12.
/**
* @param password
* 产生私钥证书的密码
* @param certSignBuf
* 公钥证书
*
* @return Base64的私钥证书
*/
public static String genP12(String password, String certSignBuf) {
// 当前userId 不存在keyMap时抛出异常
KeyStore ks = null;
X509Certificate cert = null;
ByteArrayOutputStream out = null;
try {
ks = KeyStore.getInstance("PKCS12");
ks.load(null, password.toCharArray());
cert = X509Certificate.getInstance(certSignBuf);
String alias = cert.getSerialNumber().toString(16).toUpperCase();
Certificate[] certChain = new Certificate[] { cert };
KeyPair kp = securityKP;
out = new ByteArrayOutputStream();
ks.setKeyEntry(alias, kp.getPrivate(), password.toCharArray(), certChain);
ks.store(out, password.toCharArray());
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new String(Base64.encode(out.toByteArray()));
}
Aggregations