码迷,mamicode.com
首页 > 编程语言 > 详细

java程序实现对证书的操作

时间:2015-03-14 13:52:56      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:java   证书   安全   

1 Java程序从证书文件读取证书

import java.io.*;

import java.security.cert.*;

public class PrintCert{

public static void main(String args[ ]) throws Exception{

        CertificateFactory cf=CertificateFactory.getInstance("X.509");

        FileInputStream in=new FileInputStream("my.cer");

        Certificate c=cf.generateCertificate(in);

        in.close();

        String s=c.toString( );

        // 显示证书

        FileOutputStream fout=new FileOutputStream("tmp.txt");

         BufferedWriter out= new BufferedWriter(new OutputStreamWriter(fout));

        out.write(s,0,s.length( )); 

        out.close();   

}

}  


 2 Java程序从密钥库直接读取证书

import java.io.*;

import java.security.*;

import java.security.cert.Certificate;

public class PrintCert2{

    public static void main(String args[ ]) throws Exception{

          String pass="080302";

          String alias="mykey";

          String name=".keystore";

          FileInputStream in=new FileInputStream(name);

          KeyStore ks=KeyStore.getInstance("JKS");           

          ks.load(in,pass.toCharArray());

          Certificate c=ks.getCertificate(alias);

          in.close();

         System.out.println(c.toString( ));

    }

}  


 

 

3 Java程序显示证书指定信息(全名/公钥/签名等)

import java.io.*;

import java.security.*;

import java.security.cert.*;

import java.math.*;

public class ShowCertInfo{

   public static void main(String args[ ]) throws Exception{

        CertificateFactory cf=CertificateFactory.getInstance("X.509");

        FileInputStream in=new FileInputStream("my.cer");

        java.security.cert.Certificate c=cf.generateCertificate(in);

        in.close();

        X509Certificate t=(X509Certificate) c;

        System.out.println("版本号 "+t.getVersion());

        System.out.println("序列号 "+t.getSerialNumber().toString(16));

        System.out.println("全名 "+t.getSubjectDN());

        System.out.println("签发者全名n"+t.getIssuerDN());

        System.out.println("有效期起始日 "+t.getNotBefore());

        System.out.println("有效期截至日 "+t.getNotAfter());

        System.out.println("签名算法 "+t.getSigAlgName());

        byte[] sig=t.getSignature();

        System.out.println("签名n"+new BigInteger(sig).toString(16));

        PublicKey pk=t.getPublicKey();

        byte[ ] pkenc=pk.getEncoded();

        System.out.println("公钥");

        for(int i=0;i< div=""> 

        System.out.print(pkenc[i]+",");

        }

}

}

java程序实现对证书的操作

标签:java   证书   安全   

原文地址:http://blog.csdn.net/hepfei90/article/details/44258809

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!