java程式實現對認證的操作

來源:互聯網
上載者:User

標籤: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程式實現對認證的操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.