質問

i want to create a simple java code that display all the security Providers with :

  • Name
  • info
  • service Type
  • Algorithm

Main 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());

        }
    }
}

before the second for loop i need to initialize the services to be able to call the service type and Algorithm.

役に立ちましたか?

解決

You need your second loop to be inside the first loop as you need the current provider so you can call its getServices method. Something like:

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());
        }
     }
  }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top