As part of a legacy application being upgraded to openJDK 11, I'm having difficulties refactoring bits of code relying on sun.security.* since the compilation fails with:
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) Given the below code snippet:
// 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())); ... Does anyone know how i could replace the last two lines of above code without relying on packages from sun.security.*? RSAPrivateCrtKeyImpl is using internal api sun.security.* break in openJDK11.
1 Answer
You should be able to replace the Impl class with the interface that it implements; i.e.
RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey; PublicKey publicKey = kf.generatePublic( new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPublicExponent())); since both getModulus() and getPublicExponent() are declared in the interface.
For what it is worth, this code never needed to depend on an internal implementation class in the first place.