使用Algorithm显示所有JAC安全提供程序及其服务类型

时间:2013-12-29 14:01:02

标签: java security provider

我想创建一个简单的java代码,显示所有安全提供程序:

  • 名称
  • 信息
  • 服务类型
  • 算法

主要Activity.java

import java.io.ObjectInputStream.GetField;
import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;

public class MainActivity {
    public static void main(String[] args) {

        System.out.println("Availble Providers are:");
        Provider[] providerList = Security.getProviders();
        for (int i = 0; i < providerList.length; i++) {
            System.out.println("[" + (i + 1) + "] - Name: "
                    + providerList[i].getName());

            System.out.println("Information:\n" + providerList[i].getInfo());

            System.out
                    .print("Here are all providers with types of service and algorithm provided:\n");

        }

        for (int i = 0; i < serviceList.length; i++) {
            System.out.println("- Name: " + providerList[i].getName() + "\n");
            System.out.print("Service Type: " + serviceList[i].getType()
                    + "Algorithm: " + serviceList[i].getAlgorithm());

        }
    }
}

在第二个for循环之前我需要初始化服务以便能够调用服务类型和算法。

1 个答案:

答案 0 :(得分:5)

您需要第二个循环进入第一个循环,因为您需要当前的提供程序,因此您可以调用其getServices方法。类似的东西:

import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
import java.util.Set;

public class MainActivity
{
  public static void main(String[] args)
  {
    Provider [] providerList = Security.getProviders();
    for (Provider provider : providerList)
     {
       System.out.println("Name: "  + provider.getName());
       System.out.println("Information:\n" + provider.getInfo());

       Set<Service> serviceList = provider.getServices();
       for (Service service : serviceList)
        {
          System.out.println("Service Type: " + service.getType() + " Algorithm " + service.getAlgorithm());
        }
     }
  }
}