Here's the reproducer:
-------------------
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
public class Main {
public static void main(String[] args) throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "SunMSCAPI");
KeyPair pair = generator.generateKeyPair();
Key publicKey = pair.getPublic();
pair = null;
for (int i = 0; i < 1024; ++i) {
System.gc();
}
publicKey.getEncoded(); // <<< throws KeyException: The parameter is incorrect
}
}
-------------------
This is because when a KeyPair is created both keys share the same native handles.
Thus, when one of them is destroyed (i.e. finalized), the handles become invalid.