KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > security > KeyStore

java.security
Class KeyStore

java.lang.Object
  extended by java.security.KeyStore
See Also:
Top Examples, Source Code, loaded, PrivateKey, SecretKey, Certificate

public final Enumeration<String> aliases()
                                  throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean containsAlias(String alias)
                            throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void deleteEntry(String alias)
                       throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean entryInstanceOf(String alias,
                                     Class<? extends KeyStore.Entry> entryClass)
                              throws KeyStoreException
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final Certificate getCertificate(String alias)
                                 throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final String getCertificateAlias(Certificate cert)
                                 throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final Certificate[] getCertificateChain(String alias)
                                        throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final Date getCreationDate(String alias)
                           throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final String getDefaultType()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final KeyStore.Entry getEntry(String alias,
                                     KeyStore.ProtectionParameter protParam)
                              throws NoSuchAlgorithmException,
                                     UnrecoverableEntryException,
                                     KeyStoreException
See Also:
setEntry(String, KeyStore.Entry, KeyStore.ProtectionParameter), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static KeyStore getInstance(String type)
                            throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[82]How to specify NDBS as a keystore service provider
By Anonymous on 2002/10/14 18:48:43  Rate
/** This example shows how to specify NDBS as a keystore service provider,  
   * To specify NDBS as the keystore service provider, you need to use the method  
   * java.security.KeyStore.getInstance ( "NDBS" ) . Sample code is provided in Figure 2.  
   **/
 
  
  
 if  ( args.length == 0 || args.length  >  2 )   {  
        System.out.println ( "Syntax: java ShowAliases keystore-configuration-file  [ password ] " ) ; 
         }  
 else  {  
      try  {         
      // Gets NDBS instance 
      keyStore = KeyStore.getInstance ( "NDBS" ) ; 
      // Checks if password was not provided, and if not sets //it as the empty string 
      if  ( args.length  >  1 )   {  
         password = args [ 1 ] ; 
       }  
      else  {  
        password = new String ( "" ) ; 
       }  
      // Loads configuration file 
      keyStoreFile =  ( InputStream )  new FileInputStream ( args [ 0 ]  ) ; 
           
      // Loads keystore file 
      keyStore.load ( keyStoreFile, password.toCharArray (  )  ) ; 
      // Displays number of certificates in keystore 
      System.out.println ( "File contains " + keyStore.size (  )  + " certificates." ) ; 
      // Displays certificate aliases in keystore 
      for  ( Enumeration e = keyStore.aliases (  )  ; e.hasMoreElements (  )  ; )   {  
        System.out.println ( count + ". " + e.nextElement (  )  ) ; 
        count++; 
       }  
  }  
 catch  ( Exception e )   {  
   System.out.println ( e.getMessage (  )  ) ; 
  }  
  
  
 


public static KeyStore getInstance(String type,
                                   String provider)
                            throws KeyStoreException,
                                   NoSuchProviderException
See Also:
Provider, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static KeyStore getInstance(String type,
                                   Provider provider)
                            throws KeyStoreException
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final Key getKey(String alias,
                        char[] password)
                 throws KeyStoreException,
                        NoSuchAlgorithmException,
                        UnrecoverableKeyException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final Provider getProvider()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


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


public final boolean isCertificateEntry(String alias)
                                 throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean isKeyEntry(String alias)
                         throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected KeyStore(KeyStoreSpi keyStoreSpi,
                   Provider provider,
                   String type)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void load(InputStream stream,
                       char[] password)
                throws IOException,
                       NoSuchAlgorithmException,
                       CertificateException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1557]How to export and input a key
By Anonymous on 2005/10/07 05:20:55  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 final void load(KeyStore.LoadStoreParameter param)
                throws IOException,
                       NoSuchAlgorithmException,
                       CertificateException
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void setCertificateEntry(String alias,
                                      Certificate cert)
                               throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void setEntry(String alias,
                           KeyStore.Entry entry,
                           KeyStore.ProtectionParameter protParam)
                    throws KeyStoreException
See Also:
getEntry(String, KeyStore.ProtectionParameter), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void setKeyEntry(String alias,
                              byte[] key,
                              Certificate[] chain)
                       throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void setKeyEntry(String alias,
                              Key key,
                              char[] password,
                              Certificate[] chain)
                       throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final int size()
               throws KeyStoreException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void store(OutputStream stream,
                        char[] password)
                 throws KeyStoreException,
                        IOException,
                        NoSuchAlgorithmException,
                        CertificateException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void store(KeyStore.LoadStoreParameter param)
                 throws KeyStoreException,
                        IOException,
                        NoSuchAlgorithmException,
                        CertificateException
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags