Hi,
I have a mobile phone with a SIM card that creates an RSA public key. The problem with the SIM card generated key is that it cannot directly be used to create a java.security.PublicKey compatible object using the following snippet:
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey =
keyFactory.generatePublic(new X509EncodedKeySpec(keyBytes));
The KeyFactory.generatePublic() method will throw an exception as it appears to expect keyBytes on the following algorithm-independent format:
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier,
subjectPublicKey BIT STRING
}
The algorithm part of the SubjectPublicKeyInfo is missing in the SIM card generated key. I only receive subjectPublicKey, the raw key bytes.
So my question is: Using the standard Java security API, is there a way to create a SubjectPublicKeyInfo-compliant key using my original SIM card generated key, and adding the algorithm stuff (whatever that will be).
I have found a workaround using the IAIK provider as follows:
iaik.security.rsa.RSAPublicKey rsaPublicKey =
iaik.security.rsa.RSAPublicKey.parse(keyBytes);
This object may then be cast to a java.security.PublicKey compatible object.
However, my project is not (officially) using the IAIK provider, so I would prefer to use the standard Java security API with the SunRsaSign or BC provider.
Thanks for any help!
Regards Dagbj�rn