package com.cer.test; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JFileChooser; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.awt.event.ActionEvent; /** * keytool 方式生成证书 */ public class CerFrame extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private JTextField name; private JTextField time; private JTextField cn; private JTextField ou; private JTextField l; private JTextField st; private JTextField c; private JTextField o; private JTextField RSA; private JTextField password; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { CerFrame frame = new CerFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public CerFrame() { setTitle("JAVA_CER证书制作工具"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 959, 818); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel label = new JLabel("证书密匙文件名字"); label.setBounds(84, 70, 135, 29); contentPane.add(label); name = new JTextField(); name.setBounds(235, 74, 192, 21); contentPane.add(name); name.setColumns(10); JLabel label_1 = new JLabel("自定义密匙"); label_1.setBounds(418, 32, 90, 15); contentPane.add(label_1); JLabel label_2 = new JLabel("证书密匙证书有效期"); label_2.setBounds(84, 125, 135, 29); contentPane.add(label_2); time = new JTextField(); time.setColumns(10); time.setBounds(235, 129, 192, 21); contentPane.add(time); JLabel label_3 = new JLabel("证书密匙证书主体标识(所有者-颁发者)"); label_3.setBounds(389, 159, 212, 29); contentPane.add(label_3); cn = new JTextField(); cn.setColumns(10); cn.setBounds(480, 198, 192, 21); contentPane.add(cn); JButton button = new JButton("生成密匙"); button.setBounds(313, 448, 148, 23); contentPane.add(button); // JTextArea textArea = new JTextArea(); JButton button_1 = new JButton("打开密匙位置"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { java.awt.Desktop.getDesktop().open(new File("D:\\cer")); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); button_1.setBounds(506, 448, 148, 23); contentPane.add(button_1); JFileChooser jfc=new JFileChooser(new File("D:\\cer")); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); JButton button_2 = new JButton("选择文件"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int value=jfc.showDialog(new JLabel(), "选择"); if(value==JFileChooser.APPROVE_OPTION){ //判断窗口是否点的是打开或保存 File file=jfc.getSelectedFile(); if(file.isDirectory()){ System.out.println("文件夹:"+file.getAbsolutePath()); JOptionPane.showMessageDialog(CerFrame.this, "请选择密匙文件", "系统信息", JOptionPane.INFORMATION_MESSAGE); return; }else if(file.isFile()){ System.out.println("文件:"+file.getAbsolutePath()); } System.out.println(jfc.getSelectedFile().getName()); }else{ // 没有选择,点了取消后要做些什么 } } }); button_2.setBounds(315, 495, 339, 23); contentPane.add(button_2); JButton button_3 = new JButton("生成证书"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { File file=jfc.getSelectedFile(); if(file!=null) { if(file.isDirectory()){ System.out.println("文件夹:"+file.getAbsolutePath()); JOptionPane.showMessageDialog(CerFrame.this, "请选择密匙文件", "系统信息", JOptionPane.INFORMATION_MESSAGE); return; }else if(file.isFile()){ String absolutePath = file.getAbsolutePath(); System.out.println(absolutePath.substring(absolutePath.indexOf(".")+1, absolutePath.length())); if(absolutePath.substring(absolutePath.indexOf(".")+1, absolutePath.length()).equals("keystore")) { System.out.println("ok"); System.out.println(file.getName()); String[] arstringCommand = new String[] { "cmd ", "/k", "start", // cmd Shell命令 "keytool", "-export", // - export指定为导出操作 "-keystore", // -keystore指定keystore文件,这里是d:/demo.keystore absolutePath, "-alias", // -alias指定别名,这里是ss "ss", "-file",//-file指向导出路径 "d:/cer/"+file.getName().subSequence(0, file.getName().indexOf("."))+".cer", "-storepass",// 指定密钥库的密码 "123456" }; execCommand(arstringCommand); }else { JOptionPane.showMessageDialog(CerFrame.this, "请选择keystore文件", "系统信息", JOptionPane.INFORMATION_MESSAGE); return; } System.out.println("文件:"+file.getAbsolutePath()); } }else { JOptionPane.showMessageDialog(CerFrame.this, "请选择密匙文件", "系统信息", JOptionPane.INFORMATION_MESSAGE); return; } } }); button_3.setBounds(322, 545, 151, 23); contentPane.add(button_3); JTextArea textArea_1 = new JTextArea(); textArea_1.setBounds(143, 599, 656, 140); contentPane.add(textArea_1); JButton button_4 = new JButton("查看证书信息"); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { File file=jfc.getSelectedFile(); if(file!=null) { if(file.isDirectory()){ System.out.println("文件夹:"+file.getAbsolutePath()); JOptionPane.showMessageDialog(CerFrame.this, "请选择cer证书文件", "系统信息", JOptionPane.INFORMATION_MESSAGE); return; }else if(file.isFile()){ String absolutePath = file.getAbsolutePath(); System.out.println("--"+absolutePath.substring(absolutePath.indexOf(".")+1, absolutePath.length())); if(absolutePath.substring(absolutePath.indexOf(".")+1, absolutePath.length()).equals("cer")) { X509Certificate x509Certificate = null; try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); FileInputStream fileInputStream; fileInputStream = new FileInputStream(absolutePath); x509Certificate = (X509Certificate) certificateFactory.generateCertificate(fileInputStream); fileInputStream.close(); textArea_1.setText(""); textArea_1.append("读取Cer证书信息...\r\n"); textArea_1.append("x509Certificate_SerialNumber_序列号___:"+x509Certificate.getSerialNumber()+"\r\n"); textArea_1.append("x509Certificate_getIssuerDN_发布方标识名___:"+x509Certificate.getIssuerDN()+"\r\n"); textArea_1.append("x509Certificate_getSigAlgOID_证书算法OID字符串___:"+x509Certificate.getSigAlgOID()+"\r\n"); textArea_1.append("x509Certificate_getNotBefore_证书有效期___:"+x509Certificate.getNotAfter()+"\r\n"); textArea_1.append("x509Certificate_getSigAlgName_签名算法___:"+x509Certificate.getSigAlgName()+"\r\n"); textArea_1.append("x509Certificate_getVersion_版本号___:"+x509Certificate.getVersion()+"\r\n"); System.out.println("x509Certificate_getPublicKey_公钥___:"+x509Certificate.getPublicKey()+"\r\n"); System.out.println("x509Certificate_SerialNumber_序列号___:"+x509Certificate.getSerialNumber()); System.out.println("x509Certificate_getIssuerDN_发布方标识名___:"+x509Certificate.getIssuerDN()); System.out.println("x509Certificate_getSubjectDN_主体标识___:"+x509Certificate.getSubjectDN()); System.out.println("x509Certificate_getSigAlgOID_证书算法OID字符串___:"+x509Certificate.getSigAlgOID()); System.out.println("x509Certificate_getNotBefore_证书有效期___:"+x509Certificate.getNotAfter()); System.out.println("x509Certificate_getSigAlgName_签名算法___:"+x509Certificate.getSigAlgName()); System.out.println("x509Certificate_getVersion_版本号___:"+x509Certificate.getVersion()); System.out.println("x509Certificate_getPublicKey_公钥___:"+x509Certificate.getPublicKey()); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }else { JOptionPane.showMessageDialog(CerFrame.this, "请选择cer证书", "系统信息", JOptionPane.INFORMATION_MESSAGE); return; } } }else { JOptionPane.showMessageDialog(CerFrame.this, "请选择cer证书文件", "系统信息", JOptionPane.INFORMATION_MESSAGE); return; } } }); button_4.setBounds(506, 545, 148, 23); contentPane.add(button_4); JLabel lblCn = new JLabel("CN-拥有者名字"); lblCn.setBounds(307, 198, 135, 21); contentPane.add(lblCn); JLabel lblOu = new JLabel("OU-组织机构名"); lblOu.setBounds(307, 237, 135, 21); contentPane.add(lblOu); ou = new JTextField(); ou.setColumns(10); ou.setBounds(480, 241, 192, 21); contentPane.add(ou); JLabel lblO = new JLabel("L-城市"); lblO.setBounds(307, 324, 90, 21); contentPane.add(lblO); l = new JTextField(); l.setColumns(10); l.setBounds(480, 324, 192, 21); contentPane.add(l); JLabel lblSt = new JLabel("ST-州或省"); lblSt.setBounds(307, 359, 135, 29); contentPane.add(lblSt); JLabel lblC = new JLabel("C-国家代码"); lblC.setBounds(307, 398, 135, 29); contentPane.add(lblC); st = new JTextField(); st.setColumns(10); st.setBounds(480, 359, 192, 21); contentPane.add(st); c = new JTextField(); c.setColumns(10); c.setBounds(480, 398, 192, 21); contentPane.add(c); JLabel lblO_1 = new JLabel("O-组织名"); lblO_1.setBounds(307, 276, 135, 21); contentPane.add(lblO_1); o = new JTextField(); o.setColumns(10); o.setBounds(480, 275, 192, 21); contentPane.add(o); JLabel label_4 = new JLabel("证书密匙证书签名算法"); label_4.setBounds(500, 70, 135, 29); contentPane.add(label_4); RSA = new JTextField(); RSA.setText("SHA256withRSA"); RSA.setColumns(10); RSA.setBounds(707, 74, 192, 21); contentPane.add(RSA); JLabel label_5 = new JLabel("证书密匙证书密钥库的密码"); label_5.setBounds(500, 125, 183, 29); contentPane.add(label_5); password = new JTextField(); password.setColumns(10); password.setBounds(707, 129, 192, 21); contentPane.add(password); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /** * 生成密钥 */ System.out.println("CN=(lzh),OU=(" +ou.getText()+"), O=("+o.getText()+"), L=("+l.getText()+"), ST=("+st.getText()+"), C=("+c.getText()+")"); String[] arstringCommand = new String[] { "cmd ", "/k", "start", // cmd Shell命令 "keytool", "-genkey", // -genkey表示生成密钥 "-validity", // -validity指定证书有效期(单位:天),这里是36000天 time.getText(), "-keysize",// 指定密钥长度 "1024", "-alias", // -alias指定别名,这里是ss "ss", "-keyalg", // -keyalg 指定密钥的算法 (如 RSA DSA(如果不指定默认采用DSA)) "RSA", "-sigalg" ,// -sigalg 此处”SHA256withRSA“为签名算法。 "SHA256withRSA ", "-keystore", // -keystore指定存储位置,这里是d:/demo.keystore "d:/cer/"+name.getText()+".keystore", "-dname",// CN=(名字与姓氏), OU=(组织单位名称), O=(组织名称), L=(城市或区域名称), // ST=(州或省份名称), C=(单位的两字母国家代码)" "CN=(lzh),OU=(" +ou.getText()+"), O=("+o.getText()+"), L=("+l.getText()+"), ST=("+st.getText()+"), C=("+c.getText()+")", "-storepass", // 指定密钥库的密码(获取keystore信息所需的密码) password.getText(), "-keypass",// 指定别名条目的密码(私钥的密码) password.getText(), "-v"// -v 显示密钥库中的证书详细信息 }; execCommand(arstringCommand); } }); } public void execCommand(String[] arstringCommand) { for (int i = 0; i < arstringCommand.length; i++) { System.out.print(arstringCommand[i] + " "); } try { Runtime.getRuntime().exec(arstringCommand); } catch (Exception e) { System.out.println(e.getMessage()); } } public void execCommand(String arstringCommand) { try { Runtime.getRuntime().exec(arstringCommand); } catch (Exception e) { System.out.println(e.getMessage()); } } }
最近下载更多
尹恒yingying LV18
2021年10月29日
1401170562 LV1
2020年4月24日
zjhylove LV1
2020年4月13日
ssh0 LV11
2020年3月26日
francochan LV6
2019年8月5日
2668869965 LV1
2019年7月31日
闫繁宇 LV9
2019年1月18日
best2018 LV46
2018年11月15日
走你個魯 LV21
2018年7月19日
Weipeng_ LV14
2018年6月22日