日期:2014-05-20  浏览次数:20671 次

java RSA算法实现256位密钥怎么做?
    别跟我说密钥越长越安全,现在公司指明要用256位密钥。java里面keyPairGen.initialize(1024);这里指定的长度默认为1024。我查了一下,最小只能是512位的。
查看了一下KeyPairGenerator.class这个文件:
      * <p>If the algorithm is the <i>DSA</i> algorithm, and the keysize (modulus
 * size) is 512, 768, or 1024, then the <i>Sun</i> provider uses a set of
 * precomputed values for the <code>p</code>, <code>q</code>, and
 * <code>g</code> parameters. If the modulus size is not one of the above
 * values, the <i>Sun</i> provider creates a new set of parameters. 

用256编译会报错。这个有谁遇到过,怎么解决的?

------解决方案--------------------

try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
final int KEY_SIZE = 128;// 没什么好说的了,这个值关系到块加密的大小,可以更改,但是不要太大,否则效率会低
keyPairGen.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
return keyPair;
} catch (Exception e) {
throw new Exception(e.getMessage());
}