2014-10-09 78 views
2

我正在编写一个Web服务,它将允许用户从算法列表中选择算法并生成指定大小的密钥(使用KeyGenerator)。我无法通过API找到任何方式来获取指定算法的可能密钥大小的集合。如果我尝试生成具有无效大小的密钥,那么我将得到一个异常,并在错误消息中显示该算法的可能密钥大小,但是我想要为用户选择列表预先确定它。任何人都可以为我指出正确的API,如果存在的话?获取JCE KeyGenerators的所有可能的密钥大小

回答

1

我不认为这是通过API可用,因为一些算法采用范围而不是独特的值。

Java Cryptography Architecture Documentation (Java 7),第密钥大小限制的KeyGenerator

的SunJCE提供使用以下默认keysizes(位),并执行以下限制:

KeyGenerator 
╔══════════════════════╦══════════════════╦════════════════════════════════════════════════════════════════════════════════╗ 
║ Algorithm Name  ║ Default Keysize ║        Restrictions/Comments        ║ 
╠══════════════════════╬══════════════════╬════════════════════════════════════════════════════════════════════════════════╣ 
║ AES     ║    128 ║ Keysize must be equal to 128, 192, or 256.          ║ 
║ ARCFOUR (RC4)  ║    128 ║ Keysize must range between 40 and 1024 (inclusive).       ║ 
║ Blowfish    ║    128 ║ Keysize must be a multiple of 8, ranging from 32 to 448 (inclusive).   ║ 
║ DES     ║    56 ║ Keysize must be equal to 56.             ║ 
║ DESede (Triple DES) ║    168 ║ Keysize must be equal to 112 or 168.           ║ 
║      ║     ║ A keysize of 112 will generate a Triple DES key with 2 intermediate keys, and ║ 
║      ║     ║ a keysize of 168 will generate a Triple DES key with 3 intermediate keys.  ║ 
║      ║     ║ Due to the "Meet-In-The-Middle" problem, even though 112 or 168 bits of key ║ 
║      ║     ║ material are used, the effective keysize is 80 or 112 bits respectively.  ║ 
║ HmacMD5    ║    512 ║ No keysize restriction.              ║ 
║ HmacSHA1    ║    512 ║ No keysize restriction.              ║ 
║ HmacSHA256   ║    256 ║ No keysize restriction.              ║ 
║ HmacSHA384   ║    384 ║ No keysize restriction.              ║ 
║ HmacSHA512   ║    512 ║ No keysize restriction.              ║ 
║ RC2     ║    128 ║ Keysize must range between 40 and 1024 (inclusive).       ║ 
╚══════════════════════╩══════════════════╩════════════════════════════════════════════════════════════════════════════════╝ 

我建议你为用户输入提供一个合适的组件(可编辑组合框),它随着算法选择而改变然后为其提供验证。