首页>代码>java swing制作cer证书>/Cer/src/com/cer/test/CerFrame2.java
package com.cer.test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import com.eltima.components.ui.DatePicker;

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.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.awt.event.ActionEvent;
/**
 * 开元插件生成证书
 */
public class CerFrame2 extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField name;
	private JTextField starttime;
	private JTextField cn;
	private JTextField ou;
	private JTextField l;
	private JTextField st;
	private JTextField c;
	private JTextField o;
	private JTextField num;
	private JTextField endtime;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
				/*	String substring = substring("C=china, O=university, OU=hnu, CN=huahua","C");
				System.out.println(substring);*/
					CerFrame2 frame = new CerFrame2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CerFrame2() {
		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);
		
		starttime = new JTextField();
		starttime.setColumns(10);
		
		 final DatePicker datepick;
	        datepick = getDatePicker();
	        contentPane.add(datepick);
	        datepick.setBounds(235, 129, 192, 21);
	        contentPane.add(datepick);
//		starttime.setBounds(235, 129, 192, 21);
//		contentPane.add(starttime);
		
		JLabel label_3 = new JLabel("证书密匙证书主体标识(使用者)");
		label_3.setBounds(389, 159, 246, 29);
		contentPane.add(label_3);
		
		cn = new JTextField();
		cn.setColumns(10);
		cn.setBounds(480, 198, 192, 21);
		contentPane.add(cn);
		
		endtime = new JTextField();
		endtime.setColumns(10);
		endtime.setBounds(707, 129, 192, 21);
//		contentPane.add(endtime);
		
		 final DatePicker datepick1;
	        datepick1 = getDatePicker1();
	        datepick1.setBounds(707, 129, 192, 21);
	        contentPane.add(datepick1);
	        
//		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, 512, 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) {
				
		    
		        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(CerFrame2.this,
									"请选择密匙文件", "系统信息", JOptionPane.INFORMATION_MESSAGE);
							return;
				        }else if(file.isFile()){  
				            System.out.println("文件:"+file.getAbsolutePath());  
				            
				            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(); 
										num.setText(x509Certificate.getSerialNumber()+"");
										cn.setText(substring(x509Certificate.getSubjectDN().toString(),"CN"));
										ou.setText(substring(x509Certificate.getSubjectDN().toString(),"OU"));
										o.setText(substring(x509Certificate.getSubjectDN().toString(),"O"));
										l.setText(substring(x509Certificate.getSubjectDN().toString(),"L"));
										c.setText(substring(x509Certificate.getSubjectDN().toString(),"C"));
										st.setText(substring(x509Certificate.getSubjectDN().toString(),"ST"));
									    name.setText(absolutePath.substring(absolutePath.lastIndexOf("\\")+1, absolutePath.indexOf(".")));
									
										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()); 
//										starttime.setText(t);
										starttime.setText(x509Certificate.getNotAfter().toString());
										;
										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(CerFrame2.this,
											"请选择cer证书", "系统信息", JOptionPane.INFORMATION_MESSAGE);
									return;
				        	}
				        }  
				        System.out.println(jfc.getSelectedFile().getName());  
		        	}else{
		        	// 没有选择,点了取消后要做些什么
		        	}
		        
		       
			}
		});
		button_2.setBounds(315, 482, 151, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("生成证书");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(name.getText()==null||name.getText()==""||name.getText().length()==0) {
					JOptionPane.showMessageDialog(CerFrame2.this,
							"请填写证书文件名字", "系统信息", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				 /* JOptionPane.showMessageDialog(CerFrame2.this, "获取控件中的日期:" + datepick.getValue());
	                System.out.println(datepick.getValue());//这是一个java.util.Date对象
*/				  MyCert myCert = new MyCert();
			        KeyPair keyPair_root;
					try {
						keyPair_root = myCert.generateKeyPair(10);
						KeyPair keyPair_user = myCert.generateKeyPair(100);
						String[] info = {cn.getText(),ou.getText(),o.getText(),c.getText(),l.getText(),st.getText(),"","",num.getText()};
						X509Certificate cert = myCert.generateCert(info, keyPair_root, keyPair_user, (Date)datepick.getValue(), (Date)datepick1.getValue());
						String certPath = "d:/cer/"+name.getText()+".cer";
						FileOutputStream fos = new FileOutputStream(certPath);
						fos.write(cert.getEncoded());
						fos.close();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				
			}
		});
		button_3.setBounds(315, 449, 357, 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(CerFrame2.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("");
										 Date notBefore = x509Certificate.getNotBefore();
										Date notAfter = x509Certificate.getNotAfter();
									    //创建日期格式化对象   因为DateFormat类为抽象类 所以不能new
								        DateFormat bf = new SimpleDateFormat("yyyy-MM-dd E a HH:mm:ss");//多态
								        //2017-04-19 星期三 下午 20:17:38
								     
								        String format = bf.format(notBefore);//格式化 bf.format(date);
								        String format1 = bf.format(notAfter);//格式化 bf.format(date);
								        
										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_证书开始时间___:"+format+"\r\n");
										textArea_1.append("x509Certificate_getNotBefore_证书结束时间___:"+format1+"\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(CerFrame2.this,
											"请选择cer证书", "系统信息", JOptionPane.INFORMATION_MESSAGE);
									return;
				        	}
				       
				        }  
				        
				 }else {
					 JOptionPane.showMessageDialog(CerFrame2.this,
								"请选择cer证书文件", "系统信息", JOptionPane.INFORMATION_MESSAGE);
						return;
				 }
			   
				 
				
			}
		});
		button_4.setBounds(506, 479, 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);
		
		num = new JTextField();
		num.setColumns(10);
		num.setBounds(707, 74, 192, 21);
		contentPane.add(num);
		
		JLabel label_5 = new JLabel("证书密匙证书结束时间");
		label_5.setBounds(500, 125, 183, 29);
		contentPane.add(label_5);
		
	
		
		JButton button = new JButton("修改证书");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(name.getText()==null||name.getText()==""||name.getText().length()==0) {
					JOptionPane.showMessageDialog(CerFrame2.this,
							"请填写证书文件名字", "系统信息", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				  MyCert myCert = new MyCert();
		        KeyPair keyPair_root;
				try {
					keyPair_root = myCert.generateKeyPair(10);
					KeyPair keyPair_user = myCert.generateKeyPair(100);
					String[] info = {cn.getText(),ou.getText(),o.getText(),c.getText(),l.getText(),st.getText(),"","",num.getText()};
					X509Certificate cert = myCert.generateCert(info, keyPair_root, keyPair_user, (Date)datepick.getValue(), (Date)datepick1.getValue());
					String certPath = "d:/cer/"+name.getText()+".cer";
					FileOutputStream fos = new FileOutputStream(certPath);
					fos.write(cert.getEncoded());
					fos.close();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		button.setBounds(315, 512, 151, 23);
		contentPane.add(button);
	}
	
	   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());  
	        }  
	    }  
	    private static DatePicker getDatePicker() {
	        final DatePicker datepick;
	        // 格式
	        String DefaultFormat = "yyyy-MM-dd HH:mm:ss";
	        // 当前时间
	        Date date = new Date();
	
	        // 字体
	        Font font = new Font("Times New Roman", Font.BOLD, 14);

	        Dimension dimension = new Dimension(177, 24);

	        int[] hilightDays = { 1, 3, 5, 7 };

	        int[] disabledDays = { 4, 6, 5, 9 };
	    //构造方法(初始时间,时间显示格式,字体,控件大小)
	        datepick = new DatePicker(date, DefaultFormat, font, dimension);

	        datepick.setLocation(137, 83);//设置起始位置
	        /*
	        //也可用setBounds()直接设置大小与位置
	        datepick.setBounds(137, 83, 177, 24);
	        */
	        // 设置一个月份中需要高亮显示的日子
	        datepick.setHightlightdays(hilightDays, Color.red);
	        // 设置一个月份中不需要的日子,呈灰色显示
	        datepick.setDisableddays(disabledDays);
	        // 设置国家
	        datepick.setLocale(Locale.CANADA);
	        // 设置时钟面板可见
	        datepick.setTimePanleVisible(true);
	        return datepick;
	    }
	    private static String substring(String str,String s) {
	    	str=str.substring(str.indexOf(s+"="), str.length());
	    	System.out.println("str:"+str);
	    	if(str.contains(",")) {
	    		str=str.substring(str.indexOf("=")+1, str.indexOf(","));
	    		
	    	}else {
	    		str=str.substring(str.indexOf("=")+1, str.length());
	    	}
			return str;
	    	
	    }
	    private static DatePicker getDatePicker1() {
	    	final DatePicker datepick;
	    	// 格式
	    	String DefaultFormat = "yyyy-MM-dd HH:mm:ss";
	    	// 当前时间
	    	Date date = new Date();
	    	
	    	Calendar calendar = Calendar.getInstance();
	    	
	    	calendar.setTime(date);
//	        calendar.add(Calendar.WEEK_OF_YEAR, -1);
	    	calendar.add(Calendar.YEAR, +1);
	    	date=calendar.getTime();
	    	// 字体
	    	Font font = new Font("Times New Roman", Font.BOLD, 14);
	    	
	    	Dimension dimension = new Dimension(177, 24);
	    	
	    	int[] hilightDays = { 1, 3, 5, 7 };
	    	
	    	int[] disabledDays = { 4, 6, 5, 9 };
	    	//构造方法(初始时间,时间显示格式,字体,控件大小)
	    	datepick = new DatePicker(date, DefaultFormat, font, dimension);
	    	
	    	datepick.setLocation(137, 83);//设置起始位置
	    	/*
	        //也可用setBounds()直接设置大小与位置
	        datepick.setBounds(137, 83, 177, 24);
	    	 */
	    	// 设置一个月份中需要高亮显示的日子
	    	datepick.setHightlightdays(hilightDays, Color.red);
	    	// 设置一个月份中不需要的日子,呈灰色显示
	    	datepick.setDisableddays(disabledDays);
	    	// 设置国家
	    	datepick.setLocale(Locale.CANADA);
	    	// 设置时钟面板可见
	    	datepick.setTimePanleVisible(true);
	    	return datepick;
	    }
}
最近下载更多
尹恒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日
最近浏览更多
skook7  LV2 2023年9月7日
漫步的海星  LV4 2023年8月25日
杨豫川  LV12 2022年9月5日
拾壹may 11 2022年4月13日
暂无贡献等级
忧麦紫  LV18 2022年3月2日
sswert  LV2 2022年2月22日
ᯤʸᵒᵘ  LV1 2021年12月24日
尹恒yingying  LV18 2021年10月29日
annazhang  LV29 2021年7月24日
心中无码  LV5 2021年6月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友