java将JDK8升级到OpenJKD 11:sun。安全rsa
作为升级到openJDK 11
的遗留应用程序的一部分,我很难重构依赖sun.security.*
的代码位,因为编译失败:
package sun.security.rsa is not visible
(package sun.security.rsa is declared in module java.base, which does not export it to the unnamed module)
给出以下代码片段:
// Read private key which is BASE64 encoded
byte[] encodedKey = Base64.decode(config.getPrivateKey().getBytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(keySpec);
// Illegal Access To Internal APIs
RSAPrivateCrtKeyImpl rsaPrivateKey = (RSAPrivateCrtKeyImpl) privateKey;
PublicKey publicKey = kf.generatePublic(new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPublicExponent()));
...
有人知道我如何替换上面代码的最后两行,而不依赖sun.security.*
的包吗RSAPrivateCrtKeyImpl
正在openJDK11中使用内部api sun.security.*
中断
# 1 楼答案
您应该能够用它实现的接口替换
Impl
类;i、 e因为
getModulus()
和getPublicExponent()
都是在接口中声明的值得一提的是,这段代码从一开始就不需要依赖内部实现类