I have generated a 4096-bit RSA public key for my application through the following code,
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(4096);
KeyPair pair = generator.generateKeyPair();
PublicKey publicKey = pair.getPublic();
try (FileOutputStream fos = new FileOutputStream("public.key")) {
fos.write(publicKey.getEncoded());
}
Using this public key for encryption and the corresponding private key for decryption works as expected.
File publicKeyFile = new File("public.key");
byte[] publicKeyBytes = Files.readAllBytes(publicKeyFile.toPath());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
keyFactory.generatePublic(publicKeySpec);
On the system where I generated these keys and tested the encryption and decryption, the Java version is 11 and the class of the publicKey is sun.security.rsa.RSAPublicKeyImpl
.
However, in another instance of my application, the provider is different and, the underlying implementation is org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPublicKey
. The above code loads the public key just fine and encryption works, but the modulus and exponent are different. This is naturally causing decryption issues.
I have checked that the loaded byte array is the same in both the cases. The Java version of this application is 8, however, using a sun.security.rsa.RSAPublicKeyImpl
on Java 8 also works as expected, so I don’t think that is the problem.