Skip to content

Instantly share code, notes, and snippets.

@aczire
Created July 28, 2016 18:23
Show Gist options
  • Save aczire/01185892a07b493e4c948d9d20284932 to your computer and use it in GitHub Desktop.
Save aczire/01185892a07b493e4c948d9d20284932 to your computer and use it in GitHub Desktop.
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
/**
*
*/
/**
* @author Tom James
*
*/
public class GetMyPKCS12 {
/**
* @param args
*/
public static void main(String[] args) {
String pkey = "C:\\Users\\Redirection\\myuser\\Desktop\\Crypto\\pkcs8_5_EX.pvk";
String crtFile = "C:\\Users\\Redirection\\myuser\\Desktop\\Crypto\\certum\\cerfile.cer";
String pfxFile = "C:\\Users\\Redirection\\myuser\\Desktop\\Crypto\\pkcs12_5_EX.pfx";
String pfxPass = "MySup3r53crtP@ss";
if (args.length > 0) {
pkey = args[0];
pfxFile = args[1];
crtFile = args[2];
pfxPass = args[3];
}
try {
PrivateKey key = loadPrivateKey(pkey);
System.out.println(key.serialVersionUID);
CertificateFactory fac = CertificateFactory.getInstance("X509");
Certificate certificate = loadCertificate(fac, crtFile);
PrintCertificateInfo((X509Certificate) certificate);
createKeyStore(pfxPass, pfxFile, key, certificate);
} catch (Exception e) {
System.err.println(e.toString());
}
}
/**
* @param cert
*/
private static void PrintCertificateInfo(X509Certificate cert) {
System.out.println(cert.getSerialNumber().toString(16));
System.out.println(cert.getSubjectDN());
System.out.println(cert.getIssuerDN());
}
/**
*
* @param cf
* @param certFilePath
* @return
* @throws CertificateException
* @throws IOException
*/
private static X509Certificate loadCertificate(CertificateFactory cf,
String certFilePath) throws CertificateException, IOException {
File f = new File(certFilePath);
FileInputStream in = new FileInputStream(f);
try {
X509Certificate c = (X509Certificate) cf.generateCertificate(in);
c.checkValidity();
return c;
} finally {
in.close();
}
}
/**
*
* @param keyFile
* @return
* @throws Exception
*/
private static PrivateKey loadPrivateKey(String keyFile) throws Exception {
File f = new File(keyFile);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);
}
/**
*
* @param keyStorePwd
* @param keyStoreFile
* @param privateKey
* @param certificate
* @throws Exception
*/
public static void createKeyStore(String keyStorePwd, String keyStoreFile,
PrivateKey privateKey, Certificate certificate) throws Exception {
char[] pwd = keyStorePwd.toCharArray();
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(null, pwd);
KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(
pwd);
Certificate[] certChain = new Certificate[] { certificate };
KeyStore.PrivateKeyEntry pkEntry = new KeyStore.PrivateKeyEntry(
privateKey, certChain);
ks.setEntry("keypair", pkEntry, protParam);
FileOutputStream fos = new FileOutputStream(keyStoreFile);
try {
ks.store(fos, pwd);
} finally {
fos.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment