⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 signcertframe.java~8~

📁 applet java 的编程程序
💻 JAVA~8~
📖 第 1 页 / 共 2 页
字号:
package chapter11;

import java.io.*;

import java.security.*;

import java.security.cert.*;

import java.util.*;

import java.math.*;

import sun.security.x509.*;
import javax.swing.*;
import java.awt.FlowLayout;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;

/**
 * 使用CA的证书信息签发一个证书,这一过程即通过签发者CA的证书信息对被签发者的证书进行签名。
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: aaa</p>
 *
 * @author 陈雄华
 * @version 1.0
 */
public class SignCertFrame extends JFrame {
    public SignCertFrame() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    String name ; //存放CA证书和被签证书的证书库的名字
    char[] storepass ; //存放CA证书和被签证书的证书库的访问密码
    String alias; //CA证书在证书库中的别名,这个CA的证书用来签名其它的证书
    char[] cakeypass ; //CA数字证书条目的访问密码
    String aliasName; //被签证书在证书库中的alias别名
    char[] namePass; //被签证书的条目在证书库的私钥密码
    int n ; //被签证书的有效期,以年为单位,以当前时间开始计算
    int sn ; //序列号可自己定义,这里定义的意义为2004年6月签发,是本年度CA签发的第多少个以001计算,要求唯一
    String afteraliasName; //签名后新产生的被签过名的证书在库中的别名
    char[] afterNewPass ; //签名后新产生的被签过名的证书在库的条目的私钥的密码
    JLabel jLabel1 = new JLabel();
    JTextField nameFld = new JTextField();
    JLabel jLabel2 = new JLabel();
    JTextField storepassFld = new JTextField();
    JLabel jLabel3 = new JLabel();
    JTextField aliasFld = new JTextField();
    JLabel jLabel4 = new JLabel();
    JTextField cakeypassFld = new JTextField();
    JLabel jLabel5 = new JLabel();
    JTextField aliasNameFld = new JTextField(); JLabel jLabel8 = new JLabel();
    JTextField nFld = new JTextField();
    JLabel jLabel9 = new JLabel();
    JTextField snFld = new JTextField();
    JLabel jLabel10 = new JLabel();
    JTextField afteraliasNameFld = new JTextField();
    JButton jButton1 = new JButton();
    JTextField namePassFld = new JTextField();
    JLabel jLabel6 = new JLabel();
    JLabel jLabel7 = new JLabel();
    JTextField afterNewPassFld = new JTextField();
    JLabel jLabel11 = new JLabel();
    JLabel jLabel12 = new JLabel();
    GridBagLayout gridBagLayout1 = new GridBagLayout();
    /**
     * 设置信息
     */
    private void fillSetting()
    {
        name = nameFld.getText();
        storepass = storepassFld.getText().toCharArray();
        String alias = aliasFld.getText();
        cakeypass = cakeypassFld.getText().toCharArray();
        aliasName = aliasNameFld.getText();
        namePass = namePassFld.getText().toCharArray();
        int n = (new Integer(nFld.getText())).intValue();
        int sn = (new Integer(snFld.getText())).intValue();
        afteraliasName = afteraliasNameFld.getText();
        afterNewPass = afterNewPassFld.getText().toCharArray();
    }
    /**
     * 签名证书
     */
    private void sign() {
        boolean isError = false;
        try {
            //装载证书库

            FileInputStream in = new FileInputStream(name);

            KeyStore ks = KeyStore.getInstance("JKS"); //JKS为证书库的类型

            ks.load(in, storepass);

//从证书库中读出签发者(CA)的证书

            java.security.cert.Certificate cl = ks.getCertificate(alias); //读出一个CA证书,这里的l是字母l不是数据字1

            PrivateKey privateKey = (PrivateKey) ks.getKey(alias, cakeypass); //根据别名和证书密码读出CA证书的私钥

            in.close();

//从证书库中读出的签发者(CA)的证书中提取签发者的信息

            byte[] encodl = cl.getEncoded(); //提取证书的编码,这里是字母l不是数据字1

            X509CertImpl cimpl = new X509CertImpl(encodl); //这里是字母l不是数据字1,根据证书的编码创建X509CertImpl类型的对象

//根据上面的对象获得X509CertInfo类型的对象,该对象封装了证书的全部内容。

            X509CertInfo cinfo_first =

                (X509CertInfo) cimpl.get(X509CertImpl.NAME + "." +
                                         X509CertImpl.INFO);

//然后获得X500Name类型的签发者信息

            X500Name issuer = (X500Name)

                cinfo_first.get(X509CertInfo.SUBJECT + "." +
                                CertificateIssuerName.DN_NAME);

//获取待签发的证书,即获取被签发者的证书

//可从密钥库中获取,也可从导出的证书文件中获取,这里给出两种方式 ///////////////////////////////////////////////////////////////////////////////

//方式一、采用从导出的cer文件中获取 start

///////////////////////////////////////////////////////////////////////////////

            /*
                        CertificateFactory cf = CertificateFactory.getInstance("X.509");//X.509是使用最多的一种数字证书标准

             FileInputStream in2 = new FileInputStream(cerFileName);//被签证书文件

             java.security.cert.Certificate c2 = cf.generateCertificate(in2);//生成需要被签的证书

                        in2.close();

                        byte[] encod2 = c2.getEncoded();

                        X509CertImpl cimp2 = new X509CertImpl(encod2);

//获得被签证书的详细内容,然后根据这个证书生成新证书

                        X509CertInfo cinfo_second =

             (X509CertInfo)cimp2.get(X509CertImpl.NAME+"."+X509CertImpl.INFO);

             */

///////////////////////////////////////////////////////////////////////////////

//end 方式一

///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////

//方式二、从证书库中读出被签的证书 start

///////////////////////////////////////////////////////////////////////////////

            java.security.cert.Certificate c3 = ks.getCertificate(aliasName); //从证书库中读出被签证书,然后生成新的证书

            byte[] encod3 = c3.getEncoded();

            X509CertImpl cimp3 = new X509CertImpl(encod3);

            X509CertInfo cinfo_second =

                (X509CertInfo) cimp3.get(X509CertImpl.NAME + "." +
                                         X509CertImpl.INFO); ///////////////////////////////////////////////////////////////////////////////

//end方式二

/////////////////////////////////////////////////////////////////////////////// //设置新证书的有效期,使之为当前向后n年有效,新证书的

//截止日期不能超过CA证书的有效日期

            Date beginDate = new Date();

            Calendar cal = Calendar.getInstance();

            cal.setTime(beginDate);

            cal.add(cal.YEAR, n);

            Date endDate = cal.getTime();

            CertificateValidity cv = new CertificateValidity(beginDate, endDate);

            cinfo_second.set(X509CertInfo.VALIDITY, cv);

//设置新证书的序列号

            CertificateSerialNumber csn = new CertificateSerialNumber(sn);

            cinfo_second.set(X509CertInfo.SERIAL_NUMBER, csn);

//设置新证书的签发者

            cinfo_second.set(X509CertInfo.ISSUER + "." +
                             CertificateIssuerName.DN_NAME,

                             issuer);

//新的签发者是CA的证书中读出来的

//设置新证书的算法,指定CA签名该证书所使用的算法为md5WithRSA

            AlgorithmId algorithm =

                new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);

            cinfo_second.set(CertificateAlgorithmId.NAME + "." +

                             CertificateAlgorithmId.ALGORITHM, algorithm);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -