Here is the code >>
static public void doTrustToCertificates(String certPath, String keyPath) throws Exception {
try{
char[] password = "changeit".toCharArray();
String alias = "isskey";
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(null, password);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream certstream = fullStream (certPath);
Certificate certs = cf.generateCertificate(certstream);
File keystoreFile = new File(keyPath);
// Add the certificate
keystore.setCertificateEntry(alias, certs);
// Save the new keystore contents
FileOutputStream out = new FileOutputStream(keystoreFile);
keystore.store(out, password);
out.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
private static InputStream fullStream ( String fname ) throws IOException {
FileInputStream fis = new FileInputStream(fname);
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[dis.available()];
dis.readFully(bytes);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
return bais;
}
}
static public void doTrustToCertificates(String certPath, String keyPath) throws Exception {
try{
char[] password = "changeit".toCharArray();
String alias = "isskey";
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(null, password);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream certstream = fullStream (certPath);
Certificate certs = cf.generateCertificate(certstream);
File keystoreFile = new File(keyPath);
// Add the certificate
keystore.setCertificateEntry(alias, certs);
// Save the new keystore contents
FileOutputStream out = new FileOutputStream(keystoreFile);
keystore.store(out, password);
out.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
private static InputStream fullStream ( String fname ) throws IOException {
FileInputStream fis = new FileInputStream(fname);
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[dis.available()];
dis.readFully(bytes);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
return bais;
}
}
No comments:
Post a Comment