将公钥/私钥字符串转换为 RSA xmlstring

发布于 2024-08-20 03:28:12 字数 346 浏览 2 评论 0原文

我的伙伴给了我一个RSA公钥,看起来像这样:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSK+roLfbUYf6PGNIf6Ej8R5EqSTjlRABTu7T0FxR/B6iurI80jktW9+zKu4jFnFJ8oYx24raD3x5KhJZR2VCBEEbtYpPq/5E25v+LIkFbx8sNkMvEACZdAWyeQcEookGfKDER4PGrB35bntcO2SnItTZc8BOI3jAOBXTeBO16NwIDAQAB

我知道公钥中有模数和指数,我见过一个xml RSA密钥文件;但我不知道如何从这个普通字符串中提取这些部分。

有人知道如何完成我想做的事情吗?

My partner gave me an RSA public key that looks like this:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSK+roLfbUYf6PGNIf6Ej8R5EqSTjlRABTu7T0FxR/B6iurI80jktW9+zKu4jFnFJ8oYx24raD3x5KhJZR2VCBEEbtYpPq/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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两人的回忆 2024-08-27 03:28:12

这是一个base-64编码的SubjectPublicKeyInfo(参见RFC 5280), RSA 公钥。正如您所指出的,它包含 RSA 模数和公共指数。

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.

澜川若宁 2024-08-27 03:28:12

为了完成GregS的答案,假设您使用Java作为编程语言:

  1. 您应该将Base64编码转换为二进制。有一些 Base64 解码器,例如 这个,它似乎是“公共领域”,因此可以随意重复使用。您也可以自己实现;这并不难。 Wikipedia 有相关的有用链接。
  2. 使用 SubjectPublicKeyInfo ASN.1 结构解码二进制 blob。这不是很容易(除非您掌握 ASN.1),但可以使用现有代码。特别是,Java 知道如何直接做到这一点。

Java 中的公钥解码如下所示(假设二进制 blob 位于变量 blob 中):

KeyFactory kf = KeyFactory.getInstance("RSA");
KeySpec ks = new X509EncodedKeySpec(blob);
RSAPublicKey pk = (RSAPublicKey)kf.generatePublic(ks);

并且从 RSAPublicKey 实例中,您可以获取 getModulus() 和 getPublicExponent() 方法是不言自明的。相关类位于包 java.securityjava.security.interfacesjava.security.spec 中。对于您的公钥,这会产生以下结果:

modulus         = 102645155313298195029358862270152655993457886674545775623230610032728692959011417523892551564448476401788726191516935717690886291325065114613951136136194912439244754958152467056511740824446734443711392654194943771385565670988939260236433577393483222184597978937921816958725758100559250155638540637401770719799
public exponent = 65537

To complete the answer of GregS, assuming that you use Java as a programming language:

  1. 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.
  2. 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文