Java RSAPublicKey 工厂异常

Java RSAPublicKey factory exception

将 Java 升级到 1.8 后。0_171 我在 RSA public 密钥撰写期间总是遇到以下异常:

java.security.InvalidKeyException: exponent is larger than modulus

它在升级前工作正常,有人知道如何处理它吗?

RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(exponent));
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey pub = factory.generatePublic(publicSpec); <----- EXCEPTION

我发现 Java 与 JDK-8174756 引用的 1.8.0_161 有变化:

security-libs/javax.crypto RSA public key validation

In 8u161, the RSA implementation in the SunRsaSign provider will reject any RSA public key that has an exponent that is not in the valid range as defined by PKCS#1 version 2.2. This change will affect JSSE connections as well as applications built on JCE.

JDK-8174756 (not public)

这基本上意味着模数不应该是负数。 在这种情况下,以下列方式构建 BigInteger 是很重要的:

BigInteger(1, modulus)
BigInteger(1, exponent)

更改后,它又可以工作了。