Java公钥大小

时间:2014-09-15 13:19:03

标签: java encryption rsa public-key

为了与服务进行互操作,我必须使用RSA加密某些值,其密钥大小小于Java上的最小512位。我有什么方法可以放宽这项政策吗?

BigInteger e = new BigInteger("13082845549543033994073971762152947067", 10);
BigInteger m = new BigInteger("229110545576645850236522690668306544921", 10);

RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);
KeyFactory keyFac = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFac.generatePublic(spec);

// ^^^^ java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: RSA keys must be at least 512 bits long

2 个答案:

答案 0 :(得分:1)

我已检查过您的代码,我有以下结果:

SunPKCS11-NSS RSA public key, 128 bits (id 1, session object)
modulus: 229110545576645850236522690668306544921
public exponent: 13082845549543033994073971762152947067

异常到底发生在哪里?

答案 1 :(得分:0)

您可以使用BouncyCastle提供程序。只需在代码前加上

java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

并更改行

KeyFactory keyFac = KeyFactory.getInstance("RSA");

KeyFactory keyFac = KeyFactory.getInstance("RSA", "BC");