Home » Core Java » crypto » List available cryptographic services

About Ilias Tsagklis

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

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.

    (No Ratings Yet)
    Start the discussion Views Tweet it!

    Do you want to know how to develop your skillset to become a Java Rockstar?

    Subscribe to our newsletter to start Rocking right now!

    To get you started we give you our best selling eBooks for FREE!

     

    1. JPA Mini Book

    2. JVM Troubleshooting Guide

    3. JUnit Tutorial for Unit Testing

    4. Java Annotations Tutorial

    5. Java Interview Questions

    6. Spring Interview Questions

    7. Android UI Design

     

    and many more ....

     

    Receive Java & Developer job alerts in your Area

     

    Leave a Reply

    avatar
      Subscribe  
    Notify of