I have an exception when performing a Cast, my code is as follows:
public static RSAPublicKey getKey(String filename)
throws Exception {
byte[] keyBytes = Files.readAllBytes(Paths.get(filename));
X509EncodedKeySpec spec =
new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return (RSAPublicKey)kf.generatePublic(spec);
}
Here I charge the public key and load it successfully. Then I have the cast as follows:
RSAPublicKey pubKey = (RSAPublicKey) GeneraKeysRsa.getKey(file);
CaviumRSAPublicKey key = (CaviumRSAPublicKey) pubKey;
I try to perform the cast but I get the following:
ClassCastException: sun.security.rsa.RSAPublicKeyImpl cannot be cast to com.cavium.key.CaviumRSAPublicKey
Looking for the documentation of both parties I found the following:
Here we see that it inherits from PublicKey RSAPublicKey which in turn inherits of Key, then CaviumRSAPublicKey inherits from CaviumRSAKey and is in turn from CaviumKey, it is also inherited from Key then there should be no problem when performing the cast since a Key is being made with another Key.
UPDATE 1 The issue goes like this, I have a public RSA key, this is the charge to perform "x" encryption, but I must enter this in a HSM , which is Amazon-specific, for security reasons, so you do not have to load the key from X side, but consult it directly from the HSM, but this must be Cavium to store it in the HSM, that's why I try to perform the cast but I have not succeeded so far.
They could tell me why I get that exception. Thanks and regards.