I have this little program that prints out all of the supported providers in my JDK installation but I am wondering if anyone knows how I can change this program to also print out the "strength" of each of the Providers?
import java.security.Provider; 
import java.security.Security; 
public class SecurityListings 
{ 
  public static void main(String[] args) 
  { 
    for (Provider provider : Security.getProviders()) 
    { 
      System.out.println("Provider: " + provider.getName()); 
      for (Provider.Service service : provider.getServices()) 
      { 
        System.out.println(" Algorithm: " + service.getAlgorithm());
      } 
    } 
  }
} 
Cipher.getMaxAllowedKeyLength()
Pass in the transformation and it will return the highest allowed key.
Here is an easy check
public bool isUnlimitedKeyStrength() {
    return Cipher.getMaxAllowedKeyLength("AES") == Integer.MAX_VALUE;
}
Groovy script that answers the question, thanks to help from Andrew Finnell:
import javax.crypto.Cipher
import java.security.*
import javax.crypto.*
// Groovy script
class SecurityTests {    
    static void main(String[] args) {       
        for (Provider provider : Security.getProviders())
        {
            System.out.println("Provider: " + provider.getName())
            for (Provider.Service service : provider.getServices() )
            {
                int maximum = 0;
                String alg = service.getAlgorithm()
                if ( getKeyStrength( alg ) == 2147483647 ) { 
                  System.out.println(" Algorithm: " + alg +
                    ", max" ) 
                } else {
                  System.out.println(" Algorithm: " + alg +
                    ", " + getKeyStrength( alg ) ) 
                }    
            }
        }       
    }
    static int getKeyStrength( cipher ) {       
        int max     
        try {
            max = Cipher.getMaxAllowedKeyLength( cipher)
        } catch (NoSuchAlgorithmException e) {
            e.getLocalizedMessage()
            return 0
        }       
        return max
    }
}
And a windows batch file to run it:
@echo off
set JAVA_HOME1=C:\jdk1.6.0_31
set JAVA_HOME2=C:\jdk1.6.0_16
ECHO Listings from %JAVA_HOME1%
groovy.exe --javahome %JAVA_HOME1% SecurityListings.groovy > 31.result.txt
ECHO Listings from %JAVA_HOME2%
groovy.exe --javahome %JAVA_HOME2% SecurityListings.groovy > 16.result.txt
pause
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With