返回顶部

收藏

列举使用各种加密解密算法

更多
package outofmemory.cn.snippets.core;

import java.security.Provider;
import java.security.Security;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class ListAvailableEncryptionDecryptionAlgorithms {

    public static void main(String[] args) {

        // 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('.');

                String serviceType = key.substring(0, index);

                Set<String> algorithms = getAlgorithms(serviceType);

                System.out.println(serviceType);

                for (Iterator<String> iter=algorithms.iterator(); iter.hasNext();) {
                    System.out.println("\t" + iter.next());
                }

            }

        }

    }

    private static Set<String> getAlgorithms(String serviceType) {

        Set<String> algorithms = 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(serviceType+".")) {
                    algorithms.add(key.substring(serviceType.length()+1));
                }
                else if (key.startsWith("Alg.Alias."+serviceType+".")) {
                    algorithms.add(key.substring(serviceType.length()+11));
                }

            }

        }

        return algorithms;

    }

}

标签:java,加密

收藏

0人收藏

支持

0

反对

0

发表评论