📄 framecissue.java
字号:
package cproissue;
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Enumeration;
import java.lang.reflect.Method;
import java.io.*;
import java.security.*;
import java.security.cert.*;
import java.util.Date;
import java.math.*;
import sun.security.x509.*;
import com.borland.jbcl.layout.*;
import javax.swing.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class FrameCIssue extends JFrame {
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
CertEntity ce;
public FrameCIssue() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public FrameCIssue(CertEntity ce) {
try {
jbInit();
this.ce=ce;
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(xYLayout1);
this.setTitle("申请证书");
jLabel1.setText("第三步:对产生的证书进行签名。");
jButton2.setText("取消");
jButton2.addActionListener(new FrameCIssue_jButton2_actionAdapter(this));
jButton1.addActionListener(new FrameCIssue_jButton1_actionAdapter(this));
this.getContentPane().add(jLabel1, new XYConstraints(110, 63, -1, -1));
jButton1.setText("下一步");
this.getContentPane().add(jButton2, new XYConstraints(230, 157, 83, -1));
this.getContentPane().add(jButton1, new XYConstraints(80, 157, 79, -1));
}
//第三步
public void jButton1_actionPerformed(ActionEvent e) {
try
{
//签发此用户的证书
char[] storepass = ce.stroepass.toCharArray();
char[] cakeypass = ce.capass.toCharArray();
String alias = "mykey";
String name = "mykeystore";
String userc = ""+ce.cuser+".cer";
//从密钥库读取根证书,私钥
FileInputStream in = new FileInputStream(name);
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(in, storepass);
java.security.cert.Certificate c1 = ks.getCertificate(alias);
PrivateKey caprk = (PrivateKey) ks.getKey(alias, cakeypass);
in.close();
//得到签发者的名字(根证书的持有者)
byte[] encod1 = c1.getEncoded();
X509CertImpl cimp1 = new X509CertImpl(encod1);
X509CertInfo cinfo1 = (X509CertInfo) cimp1.get(X509CertImpl.NAME + "." + X509CertImpl.INFO);
X500Name issuer = (X500Name) cinfo1.get(X509CertInfo.SUBJECT + "." +CertificateIssuerName.DN_NAME);
//获取待签发的证书
CertificateFactory cf = CertificateFactory.getInstance("X.509");
FileInputStream in2 = new FileInputStream(userc);
java.security.cert.Certificate c2 = cf.generateCertificate(in2);
in2.close();
//从待签发的证书提取证书信息
byte[] encod2 = c2.getEncoded();
X509CertImpl cimp2 = new X509CertImpl(encod2);
X509CertInfo cinfo2 = (X509CertInfo) cimp2.get(X509CertImpl.NAME + "." + X509CertImpl.INFO);
//设置签发后的证书的有效期
Date begindate = new Date();
//60 day
Date enddate = new Date(begindate.getTime() +3000 * 24 * 60 * 60 * 1000L);
CertificateValidity cv = new CertificateValidity(begindate, enddate);
cinfo2.set(X509CertInfo.VALIDITY, cv);
//设置签发后的证书的序列号
int sn = (int) (begindate.getTime() / 1000);
CertificateSerialNumber csn = new CertificateSerialNumber(sn);
cinfo2.set(X509CertInfo.SERIAL_NUMBER, csn);
//设置签发后的证书的签发者
cinfo2.set(X509CertInfo.ISSUER + "." +CertificateIssuerName.DN_NAME, issuer);
//设置签发后的证书的算法
AlgorithmId algorithm = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
cinfo2.set(CertificateAlgorithmId.NAME + "." +CertificateAlgorithmId.ALGORITHM,algorithm);
// 创建签发后的证书
X509CertImpl newcert = new X509CertImpl(cinfo2);
// 用根证书的私钥对它签名
newcert.sign(caprk, "MD5WithRSA");
// 把签名后的证书存入密钥库
ks.setCertificateEntry(""+ce.cuser+"_signed", newcert);
FileOutputStream out = new FileOutputStream("mykeystore");
ks.store(out, ce.stroepass.toCharArray());
out.close();
//进入第四步
FrameCFreeDown cfdf=new FrameCFreeDown(ce);
cfdf.setSize(400,350);
cfdf.setLocation(312,234);
cfdf.setVisible(true);
this.dispose();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
//返回主界面
public void jButton2_actionPerformed(ActionEvent e) {
this.dispose();
}
}
class FrameCIssue_jButton2_actionAdapter implements ActionListener {
private FrameCIssue adaptee;
FrameCIssue_jButton2_actionAdapter(FrameCIssue adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class FrameCIssue_jButton1_actionAdapter implements ActionListener {
private FrameCIssue adaptee;
FrameCIssue_jButton1_actionAdapter(FrameCIssue adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -