How can I find verify the encryption strength of my JDK security Providers?

☆樱花仙子☆ 提交于 2020-01-02 03:51:05

问题


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

      } 
    } 
  }

} 

回答1:


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;
}



回答2:


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


来源:https://stackoverflow.com/questions/10887030/how-can-i-find-verify-the-encryption-strength-of-my-jdk-security-providers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!