JAva操作デジタル証明書


1.keytoolで.keystoreの証明書をファイルに書き込み、そのファイルから証明書情報を読み込む
CertificateFactory cf=CertificateFactory.getInstance("X.509");
FileInputStream in=new FileInputStream("out.csr");
Certificate c=cf.generateCertificate(in); String s=c.toString();

 
2.キーストアから証明書を直接読み込む
String pass="123456";
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,pass.toCharArray());
java.security.cert.Certificate c=ks.getCertificate(alias);//alias      

 
3.JAVAプログラムは鍵庫のすべての項目をリストする
String pass="123456";
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,pass.toCharArray());
Enumeration e=ks.aliases();
while(e.hasMoreElements())
    java.security.cert.Certificate c=ks.getCertificate((Stringe.nextElement());
 

 
4.JAVAプログラム鍵庫パスワードの修正
String oldpass="123456";
String newpass="654321";
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,oldpass.toCharArray());
in.close();
FileOutputStream output=new FileOutputStream(".keystore");
ks.store(output,newpass.toCharArray());
output.close();


 
 5. JAVAプログラムキーストアエントリのパスワード変更およびエントリの追加 
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,storepass.toCharArray());
Certificate [] cchain=ks.getCertificate(alias);//            PrivateKey pk=(PrivateKey)ks.getKey(alias,oldkeypass.toCharArray());//           
ks.setKeyEntry(alias,pk,newkeypass.toCharArray(),cchain);//         ,               ,                 ,             ,           ,          ,              
FileOutputStream output=new FileOutputStream("another");
ks.store(output,storepass.toCharArray());// keystore         

6.JAVAプログラムエイリアスの検証とエントリの削除 
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,storepass.toCharArray());
ks.containsAlias("sage");//           ,    true
ks.deleteEntry("sage");//         
FileOutputStream output=new FileOutputStream(".keystore");
ks.store(output,storepass.toCharArray());// keystore        ,      


  
7.JAVAプログラムはデジタル証明書を発行する
//       CA   
FileInputStream in=new FileInputStream(".keystore");
KeyStore ks=KeyStore.getInstance("JKS");
ks.load(in,storepass.toCharArray());
java.security.cert.Certificate c1=ks.getCertificate("caroot");

//       CA   
PrivateKey caprk=(PrivateKey)ks.getKey(alias,cakeypass.toCharArray());

// CA            
byte[] encod1=c1.getEncoded();   CA     
X509CertImpl cimp1=new X509CertImpl(encod1);       X509CertImpl    
X509CertInfo cinfo1=(X509CertInfo)cimp1.get(X509CertImpl.NAME+"."+X509CertImpl.INFO);   X509CertInfo  
X500Name issuer=(X500Name)cinfo1.get(X509CertInfo.SUBJECT+"."+CertificateIssuerName.DN_NAME);   X509Name        

//        
CertificateFactory cf=CertificateFactory.getInstance("X.509");
FileInputStream in2=new FileInputStream("user.csr");
java.security.cert.Certificate c2=cf.generateCertificate(in);

//              
byte [] encod2=c2.getEncoded();
X509CertImpl cimp2=new X509CertImpl(encod2); //      X509CertImpl    
X509CertInfo cinfo2=(X509CertInfo)cimp2.get(X509CertImpl.NAME+"."+X509CertImpl.INFO); //  X509CertInfo  

//        
Date begindate=new Date(); //      
Date enddate=new Date(begindate.getTime()+3000*24*60*60*1000L); //    3000 
CertificateValidity cv=new CertificateValidity(begindate,enddate); //    
cinfo2.set(X509CertInfo.VALIDITY,cv); //     

//        
int sn=(int)(begindate.getTime()/1000); //         
CertificateSerialNumber csn=new CertificateSerialNumber(sn);
cinfo2.set(X509CertInfo.SERIAL_NUMBER,csn);

//        
cinfo2.set(X509CertInfo.ISSUER+"."+CertificateIssuerName.DN_NAME,issuer);//        

//           
AlgorithmId algorithm=new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid);
cinfo2.set(CertificateAlgorithmId.NAME+"."+CertificateAlgorithmId.ALGORITHM,algorithm);

//       CA       
X509CertImpl newcert=new X509CertImpl(cinfo2);
newcert.sign(caprk,"MD5WithRSA"); //  CA      

//         
ks.setCertificateEntry("lf_signed",newcert);
FileOutputStream out=new FileOutputStream("newstore");
ks.store(out,"newpass".toCharArray()); //           ,             


 
8.デジタル証明書の検査
//        

//  X509Certificate    
CertificateFactory cf=CertificateFactory.getInstance("X.509");
FileInputStream in1=new FileInputStream("aa.crt");
java.security.cert.Certificate c1=cf.generateCertificate(in1);
X509Certificate t=(X509Certificate)c1;
in2.close();

Date TimeNow=new Date();

//     
t.checkValidity(TimeNow);

//          CertificateFactory cf=CertificateFactory.getInstance("X.509");
FileInputStream in2=new FileInputStream("caroot.crt");
java.security.cert.Certificate cac=cf.generateCertificate(in2);
in2.close();

//  CA   
PublicKey pbk=cac.getPublicKey();
c1.verify(pbk);