KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > security > cert > X509Certificate

java.security.cert
Class X509Certificate

java.lang.Object
  extended by java.security.cert.Certificate
      extended by java.security.cert.X509Certificate
All Implemented Interfaces:
Serializable, X509Extension
See Also:
Top Examples, Source Code, CertificateFactory

public abstract void checkValidity()
                            throws CertificateExpiredException,
                                   CertificateNotYetValidException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1559]How to export the first available key entry from a JKS key store and import it into a PKCS#12 key store
By Anonymous on 2005/10/07 05:22:58  Rate
//Here's an example of how to export the first available key entry from a JKS key store and import it into a PKCS#12 key store: 
  
  
 import java.security.cert.*; 
 import java.security.*; 
 import java.security.interfaces.RSAPrivateKey; 
 import java.io.*; 
  
  
 public class JKS2PKCS12 
  {  
     static String JKS_FILENAME = "mykeystore.jks"; 
     static char [  ]  JKS_PASSWORD = "secret".toCharArray (  ) ; 
  
  
     static String PKCS12_FILENAME = "mynewkeystore.p12"; 
     static char [  ]  PKCS12_PASSWORD = "secret".toCharArray (  ) ; 
  
  
     static RSAPrivateKey PRIVATE_KEY = null; 
     static X509Certificate [  ]  CERT_CHAIN = null; 
  
  
     static public void main ( String [  ]  args )  
      {  
         try  {  
             exportFromJKS (  ) ; 
             importIntoPKCS12 (  ) ; 
          }  catch ( Throwable t )   {  
             t.printStackTrace (  ) ; 
             System.err.println ( "Prog failed. Exiting..." ) ; 
          }  
      }  
  
  
     static void exportFromJKS (  )  
         throws Exception 
      {  
         //Load the JKS key store. 
         KeyStore ks = KeyStore.getInstance ( "JKS" ) ; 
         FileInputStream fis = new FileInputStream ( JKS_FILENAME ) ; 
         ks.load ( fis, JKS_PASSWORD ) ; 
         fis.close (  ) ; 
  
  
         //Get first available key entry from the JKS file. 
         String alias; 
         for ( java.util.Enumeration e = ks.aliases (  ) ; e.hasMoreElements (  ) ;  )  
              {  
                 alias =  ( String ) e.nextElement (  ) ; 
                 if ( ks.isKeyEntry ( alias )  )  
                      {  
                         PRIVATE_KEY =  ( RSAPrivateKey ) ks.getKey ( alias, JKS_PASSWORD ) ; 
                         java.security.cert.Certificate [  ]  chain = ks.getCertificateChain ( alias ) ; 
                         CERT_CHAIN = new X509Certificate [ chain.length ] ; 
  
  
                         for ( int i = 0; i  <  chain.length; i++ )  
                             CERT_CHAIN [ i ]  =  ( X509Certificate ) chain [ i ] ; 
                         System.out.println ( "Exporting key entry with alias: " + alias ) ; 
                         break; 
                      }  
              }  
         if ( CERT_CHAIN == null )  
             throw new Exception ( "Sorry, no key entry found in JKS file: " + JKS_FILENAME ) ; 
      }  
  
  
     static void importIntoPKCS12 (  )  
         throws Exception 
      {  
         //Create a PKCS#12 key store. 
         KeyStore ks = KeyStore.getInstance ( "pkcs12" ) ; 
         ks.load ( null, null ) ; 
  
  
         //Import/insert the exported key entry into PKCS#12 key store. 
         ks.setKeyEntry ( "", PRIVATE_KEY, PKCS12_PASSWORD, CERT_CHAIN ) ; 
  
  
         //Save key store on disk. 
         FileOutputStream fos = new FileOutputStream ( PKCS12_FILENAME ) ; 
         ks.store ( fos, PKCS12_PASSWORD ) ; 
         fos.close (  ) ; 
  
  
         System.out.println ( "Import of key entry into PKCS#12 key store succeeded." ) ; 
      }   
  }     


public abstract void checkValidity(Date date)
                            throws CertificateExpiredException,
                                   CertificateNotYetValidException
See Also:
checkValidity()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int getBasicConstraints()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public List<String> getExtendedKeyUsage()
                                 throws CertificateParsingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Collection<List<?>> getIssuerAlternativeNames()
                                              throws CertificateParsingException
See Also:
getSubjectAlternativeNames
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract Principal getIssuerDN()
See Also:
getIssuerX500Principal()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract boolean[] getIssuerUniqueID()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public X500Principal getIssuerX500Principal()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract boolean[] getKeyUsage()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract Date getNotAfter()
See Also:
checkValidity(), getNotBefore
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract Date getNotBefore()
See Also:
checkValidity()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract BigInteger getSerialNumber()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract String getSigAlgName()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract String getSigAlgOID()
See Also:
getSigAlgName
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract byte[] getSigAlgParams()
See Also:
getSigAlgName, AlgorithmParameters
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract byte[] getSignature()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Collection<List<?>> getSubjectAlternativeNames()
                                               throws CertificateParsingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract Principal getSubjectDN()
See Also:
getIssuerDN, getSubjectX500Principal()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract boolean[] getSubjectUniqueID()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public X500Principal getSubjectX500Principal()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract byte[] getTBSCertificate()
                                  throws CertificateEncodingException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int getVersion()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected X509Certificate()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags