My partner gave me an RSA public key that looks like this:
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSK+roLfbUYf6PGNIf6Ej8R5EqSTjlRABTu7T0FxR/B6iurI80jktW9+zK开发者_开发问答u4jFnFJ8oYx24raD3x5KhJZR2VCBEEbtYpPq/5E25v+LIkFbx8sNkMvEACZdAWyeQcEookGfKDER4PGrB35bntcO2SnItTZc8BOI3jAOBXTeBO16NwIDAQAB
I know that there are Modulus and Exponent in a public key, I've seen an xml RSA key file; but i don't know how to extract those parts from this plain string.
Would anyone know how to accomplish what I am trying to do?
That is a base-64 encoded SubjectPublicKeyInfo (see RFC 5280), for an RSA public key. It contains, as you noted, an RSA modulus and public exponent.
To complete the answer of GregS, assuming that you use Java as a programming language:
- You should transform the Base64 encoding into binary. There are some Base64 decoders out there, e.g. this one, which appears to be "public domain", thus reusable at will. You could also implement it yourself; it is not hard. Wikipedia has the useful links for that.
- Decode the binary blob using the
SubjectPublicKeyInfo
ASN.1 structure. This is not very easy (unless you master ASN.1) but existing code can be used for that. In particular, Java knows how to do that directly.
Public key decoding in Java looks like this (assuming the binary blob is in variable blob
):
KeyFactory kf = KeyFactory.getInstance("RSA");
KeySpec ks = new X509EncodedKeySpec(blob);
RSAPublicKey pk = (RSAPublicKey)kf.generatePublic(ks);
and from the RSAPublicKey
instance, you have the getModulus()
and getPublicExponent()
methods which are self-explanatory. The relevant classes are in packages java.security
, java.security.interfaces
and java.security.spec
. For your public key, this yields the following:
modulus = 102645155313298195029358862270152655993457886674545775623230610032728692959011417523892551564448476401788726191516935717690886291325065114613951136136194912439244754958152467056511740824446734443711392654194943771385565670988939260236433577393483222184597978937921816958725758100559250155638540637401770719799
public exponent = 65537
精彩评论