小编给大家分享一下java中加密解密与数字证书的操作有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
1 Java程序实现密钥库的维护
1.1 Java程序列出密钥库所有条目
import java.util.*; import java.io.*; import java.security.*; public class ShowAlias{ public static void main(String args[ ]) throws Exception{ String pass="080302"; String name=".keystore"; FileInputStream in=new FileInputStream(name); KeyStore ks=KeyStore.getInstance("JKS"); ks.load(in,pass.tochararray()); Enumeratione=ks.aliases( ); while( e.hasMoreElements()) { System.out.println(e.nextElement()); } } }
1.2 Java程序修改密钥库口令
import java.io.*; import java.security.*; public class SetStorePass{ public static void main(String args[ ]) throws Exception{ char[ ] oldpass="080302".tochararray(); char[ ] newpass="123456".tochararray(); String name=".keystore"; FileInputStream in=new FileInputStream(name); KeyStore ks=KeyStore.getInstance("JKS"); ks.load(in,oldpass); in.close(); FileOutputStream output=new FileOutputStream(name); ks.store(output,newpass); output.close(); } }
package test; import java.io.*; import java.security.*; import java.security.cert.Certificate; public class SetKeyPass{ public static void main(String args[ ]) throws Exception{ //读取相关参数 String name=".keystore"; String alias="mykey"; char[ ] storepass="123456".tochararray(); char[ ] oldkeypass="080302".tochararray(); char[ ] newkeypass="123456".tochararray(); //获取密钥库.keystore的KeyStore对象,并加载密钥库 FileInputStream in=new FileInputStream(name); KeyStore ks=KeyStore.getInstance("JKS"); ks.load(in,storepass); //获取别名对应的条目的证书链 Certificate[ ] cchain=ks.getCertificateChain(alias); //读取别名对应的条目的私钥 PrivateKey pk=(PrivateKey)ks.getKey(alias,oldkeypass); //向密钥库中添加新的条目 ks.setKeyEntry(alias,pk,newkeypass,cchain); in.close(); //将KeyStore对象内容写入新文件 FileOutputStream output=new FileOutputStream("333"); ks.store(output,storepass); output.close(); } }
1.4 Java程序检验别名及删除条目
package test; import java.io.*; import java.security.*; public class Deletealias{ public static void main(String args[ ]) throws Exception{ String pass = "123456"; String name = ".keystore"; String alias = "mykey"; FileInputStream in=new FileInputStream(name); KeyStore ks=KeyStore.getInstance("JKS"); ks.load(in,pass.tochararray()); if (ks.containsAlias(alias)){ ks.deleteEntry(alias); FileOutputStream output=new FileOutputStream(name); ks.store(output,pass.tochararray()); System.out.println("Alias "+alias+" deleted"); }else{ System.out.println("Alias not exist"); } } }
2 Java程序读取证书和显示证书指定信息
2.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.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( )); } }
2.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中加密解密与数字证书的操作有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程之家行业资讯频道!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。