RSA:获取给定公钥的指数和模数
我需要在 JavaScript 中使用 RSA 加密一些数据。周围的所有库都要求提供指数和模数,但我从对手那里得到了一个 public.key
文件。
如何从 RSA 文件中检索公共指数
和模
部分?
I need to encrypt some data using RSA in JavaScript. All of the libraries around ask for an exponent and a modulus, yet I get a single public.key
file from my opponent.
How do you retrieve the public exponent
and modulus
part from an RSA file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这取决于您可以使用的工具。我怀疑是否有 JavaScript 可以直接在浏览器中执行此操作。它还取决于它是否是一次性的(始终相同的密钥)或是否需要编写脚本。
命令行/OpenSSL
如果您想在 unix 命令行上使用 OpenSSL 之类的东西,您可以执行以下操作。
我假设您的 public.key 文件包含类似这样的内容:
然后,命令将是:
然后,您可以查看 ASN.1 结构:
这应该给您类似这样的内容:
模数和公共指数位于最后位字符串,偏移量 19,因此使用
-strparse
:这将为您提供十六进制的模数和公共指数(两个整数):
如果它始终是相同的密钥,那可能没问题,但这是放入脚本可能不太方便。
或者(这可能更容易放入脚本中),
将返回:
Java
这取决于输入格式。如果它是密钥库中的 X.509 证书,请使用
(RSAPublicKey)cert.getPublicKey()
:此对象有两个用于模数和指数的 getter。如果它的格式如上,您可能需要使用 BouncyCastle 及其
PEMReader
阅读它。我还没有尝试过以下代码,但这或多或少看起来像这样:(您也可以在 C# 中类似地使用 BouncyCastle。)
It depends on the tools you can use. I doubt there is a JavaScript too that could do it directly within the browser. It also depends if it's a one-off (always the same key) or whether you need to script it.
Command-line / OpenSSL
If you want to use something like OpenSSL on a unix command line, you can do something as follows.
I'm assuming you public.key file contains something like this:
Then, the commands would be:
Then, you can look into the ASN.1 structure:
This should give you something like this:
The modulus and public exponent are in the last BIT STRING, offset 19, so use
-strparse
:This will give you the modulus and the public exponent, in hexadecimal (the two INTEGERs):
That's probably fine if it's always the same key, but this is probably not very convenient to put in a script.
Alternatively (and this might be easier to put into a script),
will return this:
Java
It depends on the input format. If it's an X.509 certificate in a keystore, use
(RSAPublicKey)cert.getPublicKey()
: this object has two getters for the modulus and the exponent.If it's in the format as above, you might want to use BouncyCastle and its
PEMReader
to read it. I haven't tried the following code, but this would look more or less like this:(You can use BouncyCastle similarly in C# too.)
使用时请注意模数中可能出现的前导 00:
示例模数包含 257 字节而不是 256 字节,因为 00 被包含在内,因为 98 中的 9 看起来像负号数字。
Beware the leading 00 that can appear in the modulus when using:
The example modulus contains 257 bytes rather than 256 bytes because of that 00, which is included because the 9 in 98 looks like a negative signed number.
主要供我自己参考,以下是如何从 ssh-keygen 生成的私钥中获取它
当然,这仅适用于私钥。
Mostly for my own reference, here's how you get it from a private key generated by ssh-keygen
Of course, this only works with the private key.
您可以通过
Bonus 直接打印模数:在末尾添加“| openssl md5”以获得更小的字符串以便于比较
you can directly print modulus by
Bonus: add " | openssl md5" at the end to get a smaller string to compare easily
除了上面的答案之外,我们还可以使用
asn1parse
来获取值现在,为了获取此偏移量,我们尝试默认的 asn1parse
我们需要获取 BIT 字符串部分,因此我们添加大小
< code>depth_0_header(4) + height_1_full_size(2 + 13) + Container_1_EOC_bit + BIT_STRING_header(4) = 24
这可以在以下位置更好地可视化:ASN.1 Parser, if you hover at tags, you will see the offsets
Another amazing resource: Microsoft 的 ASN.1 文档
Apart from the above answers, we can use
asn1parse
to get the valuesNow, to get to this offset,we try the default asn1parse
We need to get to the BIT String part, so we add the sizes
depth_0_header(4) + depth_1_full_size(2 + 13) + Container_1_EOC_bit + BIT_STRING_header(4) = 24
This can be better visialized at: ASN.1 Parser, if you hover at tags, you will see the offsets
Another amazing resource: Microsoft's ASN.1 Docs
如果您需要解析脚本中的 ASN.1 对象,可以使用一个库: https://github.com /lapo-luchini/asn1js
为了进行数学计算,我发现 jsbn 很方便: http://www-cs-students.stanford.edu/~tjw/jsbn/
遍历 ASN.1 结构并提取 exp/mod/subject/etc。取决于你——我从来没有走到这一步!
If you need to parse ASN.1 objects in script, there's a library for that: https://github.com/lapo-luchini/asn1js
For doing the math, I found jsbn convenient: http://www-cs-students.stanford.edu/~tjw/jsbn/
Walking the ASN.1 structure and extracting the exp/mod/subject/etc. is up to you -- I never got that far!
我设法找到这个解决方案的答案,必须执行javascript注入以安装 atob
I manage to find the answer for this solution, have to do javascript injection for this to install atob