获取JCE KeyGenerators的所有可能密钥大小

时间:2014-10-09 03:05:00

标签: java jce

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

1 个答案:

答案 0 :(得分:1)

我不认为这可以通过API获得,因为有些算法采用范围而不是不同的值。

来自Java Cryptography Architecture Documentation (Java 7),部分 Keysize限制 KeyGenerator

  

SunJCE提供程序使用以下默认密钥大小(以位为单位)并强制执行以下限制:

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).                            ║
╚══════════════════════╩══════════════════╩════════════════════════════════════════════════════════════════════════════════╝

我建议你提供一个合适的用户输入组件(可编辑的组合框),它可以根据算法选择进行更改,然后为其提供验证。