📄 signcertframe.java~8~
字号:
//创建新的签名后的证书
X509CertImpl newcert = new X509CertImpl(cinfo_second);
//签名,使用CA证书的私钥进行签名,签名使用的算法为MD5WithRSA
newcert.sign(privateKey, "MD5WithRSA"); //这样便得到了经过CA签名后的证书
//把新证书存入证书库
//把新生成的证书存入一个新的证书库,也可以存入原证书库,
//存入新证书库,则新证书库中不仅包含原证书库中的所有条目,
//而且新增加了一个这次产生的条目。注意,这时,新产生的签名后的证书只
//包括公钥和主体信息及签名信息,不包括私钥信息。这里给出两种方式。
///////////////////////////////////////////////////////////////////////////
//方式一:存入新密钥库
///////////////////////////////////////////////////////////////////////////
/*
ks.setCertificateEntry(afteraliasName,newcert);
FileOutputStream out = new FileOutputStream(newLib);
//存入新库signedLib,并设置新库的库访问密码
ks.store(out,newLibPass);
out.close();
*/
///////////////////////////////////////////////////////////////////////////
//end 方式一
///////////////////////////////////////////////////////////////////////////
//也可以采用另外一种方式,存入原证书库中
//存入原库中,即在原证书库中增加一条证书,这个证书是原证书经过签名后的证书
//这个新证书含有私钥和私钥密码
///////////////////////////////////////////////////////////////////////////
//方式二,存入原密钥库
///////////////////////////////////////////////////////////////////////////
//先在原库中读出被签证书的私钥
PrivateKey prk = (PrivateKey) ks.getKey(aliasName, namePass);
java.security.cert.Certificate[] cchain = {newcert};
//存入原来的库,第二个参数为原证书的私钥,第三个参数为新证书的私钥密码,第三个参数为新证书
ks.setKeyEntry(afteraliasName, prk, afterNewPass, cchain); //用新密钥替代原来的没有签名的证书的密码
FileOutputStream out2 = new FileOutputStream(name);
ks.store(out2, storepass); //存入原来的库中,第二个参数为该库的访问密码
///////////////////////////////////////////////////////////////////////////
//end 方式二
///////////////////////////////////////////////////////////////////////////
} catch (SignatureException ex) {
isError = true;
ex.printStackTrace();
} catch (NoSuchProviderException ex) {
isError = true;
ex.printStackTrace();
} catch (InvalidKeyException ex) {
isError = true;
ex.printStackTrace();
} catch (UnrecoverableKeyException ex) {
isError = true;
ex.printStackTrace();
} catch (CertificateException ex) {
isError = true;
ex.printStackTrace();
} catch (NoSuchAlgorithmException ex) {
isError = true;
ex.printStackTrace();
} catch (IOException ex) {
isError = true;
ex.printStackTrace();
} catch (KeyStoreException ex) {
isError = true;
ex.printStackTrace();
}
if (isError) {
JOptionPane.showMessageDialog(this, "失败");
} else {
JOptionPane.showMessageDialog(this, "成功");
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(gridBagLayout1); jLabel1.setText("CA证书所在库密码");
nameFld.setText(""); jLabel2.setText("CA证书密码"); jLabel3.setText(
"CA证书别名"); jLabel4.setText("证书库名"); jLabel5.setText("被签证书别名");
jLabel8.setText("有效期(年)"); jLabel9.setText("签发序列号");
jLabel10.setText(
"证书被签后的别名"); jButton1.setText("签名"); jButton1.addActionListener(new
SignCert_jButton1_actionAdapter(this)); jLabel6.setText("被签证书私钥密码");
jLabel7.setText("被签后证书的私钥密码"); afterNewPassFld.setToolTipText("");
afterNewPassFld.setText(""); jLabel11.setText("int型"); jLabel12.setText(
"int型"); this.getContentPane().add(
jButton1, new GridBagConstraints(1, 10, 1, 1, 0.0, 0.0
, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(20, 0, 38, 10), 151, 0)); this.getContentPane().add(
jLabel6, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(14, 13, 0, 11), 0, 0)); this.
getContentPane().add(jLabel10,
new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(13, 14, 0, 10), 0, 0)); this.getContentPane().add(
jLabel1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(15, 14, 0, 6), 0, 0)); this.
getContentPane().add(jLabel3,
new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(13, 45, 0, 8), 0, 0)); this.getContentPane().add(jLabel2,
new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(18, 44, 0, 9), 0, 0)); this.
getContentPane().add(jLabel4,
new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(13, 59, 0, 9), 0, 0)); this.getContentPane().add(jLabel5,
new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(20, 37, 0, 9), 0, 0)); this.
getContentPane().add(jLabel8,
new GridBagConstraints(0, 8, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(13, 54, 0, 6), 0, 0)); this.getContentPane().add(jLabel9,
new GridBagConstraints(0, 9, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(12, 49, 0, 8), 0, 0)); this.
getContentPane().add(jLabel7,
new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(10, 2, 0, 0), 0, 0)); this.getContentPane().add(
cakeypassFld, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(16, 6, 0, 0), 206,
0)); this.getContentPane().add(aliasNameFld,
new GridBagConstraints(1, 4, 1, 1,
1.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(17, 0, 0, 0), 206, 0)); this.getContentPane().add(nFld,
new GridBagConstraints(1, 8, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(9, 0, 0, 0), 208, 0)); this.
getContentPane().add(snFld,
new GridBagConstraints(1, 9, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(8, 0, 0, 0), 208, 0)); this.getContentPane().add(nameFld,
new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(13, 6, 0, 0), 206, 0)); this.
getContentPane().add(storepassFld,
new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(13, 0, 0, 0), 206, 0)); this.getContentPane().add(
aliasFld, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(10, 0, 0, 0), 206, 0)); this.getContentPane().add(
afteraliasNameFld, new GridBagConstraints(1, 6, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(8, 0, 0, 0), 209, 0)); this.getContentPane().add(
afterNewPassFld, new GridBagConstraints(1, 7, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 208, 0)); this.getContentPane().add(
namePassFld, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(10, 0, 0, 0), 208, 0)); this.getContentPane().add(
jLabel12, new GridBagConstraints(2, 9, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(12, 0, 0, 46), 0, 0)); this.
getContentPane().add(jLabel11,
new GridBagConstraints(2, 8, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(11, 0, 0, 46), 0, 0)); }
public void jButton1_actionPerformed(ActionEvent e) {
fillSetting();
sign();
}
public static void main(String[] args) {
SignCertFrame frm = new SignCertFrame();
frm.setSize(600,450);
frm.show();
}
}
class SignCert_jButton1_actionAdapter implements ActionListener {
private SignCertFrame adaptee;
SignCert_jButton1_actionAdapter(SignCertFrame adaptee) {
this.adaptee = adaptee; }
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -