Java Tutorial/Security/JKS
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
JKS keystore: get certificate chain
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.CertPath;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.List;
public class MainClass {
public static void main(String args[]) throws Exception {
String storename = args[0];
char[] storepass = args[1].toCharArray();
String alias = args[2];
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(storename), storepass);
java.security.cert.Certificate[] cchain = ks.getCertificateChain(alias);
List mylist = new ArrayList();
for (int i = 0; i < cchain.length; i++) {
mylist.add(cchain[i]);
}
CertificateFactory cf = CertificateFactory.getInstance("X.509");
CertPath cp = cf.generateCertPath(mylist);
System.out.println(cp);
}
}
JKS: List aliases
import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Enumeration;
public class MainClass {
public static void main(String args[]) throws Exception {
String pass = "wshr.ut";
String name = "mykeystore";
FileInputStream in = new FileInputStream(name);
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(in, pass.toCharArray());
Enumeration e = ks.aliases();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}
}
Use JKS
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
public class MainClass {
public static void main(String args[]) throws Exception {
String pass = "wshr.ut";
String alias = "mytest";
String name = "mykeystore";
FileInputStream in = new FileInputStream(name);
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(in, pass.toCharArray());
Certificate c = ks.getCertificate(alias);
in.close();
System.out.println(c);
}
}
Use JKS KeyStore to load key
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyStore;
public class MainClass {
public static void main(String args[]) throws Exception {
char[] oldpass = args[0].toCharArray();
char[] newpass = args[1].toCharArray();
String name = "mykeystore";
FileInputStream in = new FileInputStream(name);
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(in, oldpass);
in.close();
FileOutputStream output = new FileOutputStream(name);
ks.store(output, newpass);
output.close();
}
}