import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class search {
public static ArrayList<File> filelist=new ArrayList<File>();
public static void main(String args[ ]) throws CertificateException, IOException{
Scanner scanner = new Scanner(System.in);
System.out.println("请输入路径");
String path=scanner.nextLine();
System.out.println("你输入的路径为"+path);
File dir = new File(path);
File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
if (files == null) {
System.out.println("路径输入错误");//该文件目录下没有东西,则输出路径输入错误
return;
}
filelist=(ArrayList<File>) getFileList(path);
if (filelist.size()==0) {
System.out.println("没有数字证书");
}
show(filelist);
}
public static List<File> getFileList(String strPath) {
File dir = new File(strPath);
File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
if (files != null) {
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
if (files[i].isDirectory() && !files[i].getName().equals("$RECYCLE.BIN")) { // 判断是文件还是文件夹,排除回收箱文件夹
getFileList(files[i].getAbsolutePath()); // 获取文件绝对路径
} else if (fileName.endsWith("cer")||fileName.endsWith("pem")||fileName.endsWith("crt")||fileName.endsWith("der")) { // 判断文件名是否以.cer,.pem.crt.der结尾
String strFileName = files[i].getAbsolutePath();
System.out.println("数字证书位置---" + strFileName);
filelist.add(files[i]);
} else {
continue;
}
}
}
return filelist;
}
public static void show(ArrayList<File> filelist) throws CertificateException, IOException{
for (File List1 : filelist) {
CertificateFactory cf=CertificateFactory.getInstance("x.509");
System.out.println("数字证书名称"+List1.getName());
System.out.println("----------数字证书名称详细信息-----------");
FileInputStream in=new FileInputStream(List1);
java.security.cert.Certificate c=cf.generateCertificate(in);
in.close();
X509Certificate t=(X509Certificate) c;
System.out.println("版本号 "+t.getVersion());
System.out.println("序列号 "+t.getSerialNumber().toString(16));
System.out.println("签名算法 "+t.getSigAlgName());
System.out.println("全名 "+t.getSubjectDN());
System.out.println("颁发者"+t.getIssuerDN());
System.out.println("有效期起始日 "+t.getNotBefore());
System.out.println("有效期截至日 "+t.getNotAfter());
}
}
}