crypto

List available cryptographic services

In this example we shall show you how to list all the available cryptographic services. To list all the available cryptographic services one should perform the following steps:

  • Create a new Set of String elements, to hold the services.
  • Use getProviders() API method of Security to get an array of the Providers.
  • For every Provider get a view of the property keys contained in this provider, using keySet() API method of Provider.
  • Iterate over the keys. For every key strip the alias. Get the part of the key that contains the service and add it to the Set of the services,
  • as described in the code snippet below.
     

    package com.javacodegeeks.snippets.core;
    
    import java.security.Provider;
    import java.security.Security;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;
    
    public class ListAvailableCryptographicServices {
    	
    	public static void main(String[] args) {
    		
    		Set<String> serviceTypes = new TreeSet<String>();
    
    	    // get an array containing all the installed providers
    	    Provider[] providers = Security.getProviders();
    	    
    	    for (int i=0; i<providers.length; i++) {	    	
    	
      // get a view of the property keys contained in this provider	
      Set<Object> keys = providers[i].keySet();	
      for (Iterator<Object> it=keys.iterator(); it.hasNext();) {
    	String key = it.next().toString();
    	key = key.split(" ")[0];
    if (key.startsWith("Alg.Alias.")) {
        // strip the alias
        key = key.substring(10);
    }
    int index = key.indexOf('.');
    serviceTypes.add(key.substring(0, index));
      }
        }
    	    for (Iterator<String> it=serviceTypes.iterator(); it.hasNext();) {
    	    	System.out.println(it.next());
    	    }
    	}
    }
    

    Output:

    AlgorithmParameterGenerator
    AlgorithmParameters
    CertPathBuilder
    CertPathValidator
    CertStore
    CertificateFactory
    Cipher
    Configuration
    GssApiMechanism
    KeyAgreement
    KeyFactory
    KeyGenerator
    KeyInfoFactory
    KeyManagerFactory
    KeyPairGenerator
    KeyStore
    Mac
    MessageDigest
    Policy
    Provider
    SSLContext
    SaslClientFactory
    SaslServerFactory
    SecretKeyFactory
    SecureRandom
    Signature
    TerminalFactory
    TransformService
    TrustManagerFactory
    XMLSignatureFactory
    

     
    This was an example of how to list all the available cryptographic services in Java.

    Ilias Tsagklis

    Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
    Subscribe
    Notify of
    guest

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    0 Comments
    Oldest
    Newest Most Voted
    Inline Feedbacks
    View all comments
    Back to top button