如何在 Java 中获取 RSA 密钥的大小
给定一个 java.security.interfaces.RSAKey,如何获取它的大小?
Given an java.security.interfaces.RSAKey, how do I get it's size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
给定一个 java.security.interfaces.RSAKey,如何获取它的大小?
Given an java.security.interfaces.RSAKey, how do I get it's size?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
你可以试试这个:
You could try this:
(编辑:在我理解对 RSA 密钥生成的素数整数的限制之前,我写了这个回复。 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf 我现在相信任何好的密钥生成器都应该确保模数介于 2^(n-1) 和 2^n-1 之间。因此,模数的最小补码表示始终具有在创建密钥时为密钥长度指定的位数。例如,如果您创建一个 2048 位密钥,则 key.getModulus().bitLength() 将始终返回 2048。)
请原谅,但
key.getModulus().bitLength() 不会返回
当模数的最高有效位为 0 时返回错误值?例如,对于 2048 位密钥,如果模数的最高有效位为 0,则 key.getModulus().bitLength() 将返回 2047(如果更多位为 0,则返回更少) 。我认为在这种情况下所需的结果实际上是 2048。BigInteger.bitLength() 内容如下:
恐怕需要对密钥的大小做出一些假设。例如,您必须假设您只会看到 1024、2048 或 4096 位密钥,然后执行以下操作:
此代码在(非常罕见)场合仍然可能是错误的,例如,当2048 位密钥的前 1048 位都是 0。不过,我认为这不是值得担心的事情。
(EDIT: I wrote this response before I understood the restrictions placed on the prime integers that are generated for an RSA key. http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf I now believe that any good key generator should ensure that the modulus is between 2^(n-1) and 2^n-1. Thus the minimal two's-complement representation of the modulus will always have exactly the number of bits that were specified for the key length at the time of key creation. So, for example, if you create a 2048-bit key, then key.getModulus().bitLength() will always return 2048.)
Pardon, but doesn't
key.getModulus().bitLength()
return an incorrect value when the most significant bit of the modulus is a 0? For example, for a 2048-bit key, if the most significant bit of the modulus is 0, thenkey.getModulus().bitLength()
will return 2047 (or less if more bits are 0). I would think the desired result in such a case would actually be 2048.The documentation for BigInteger.bitLength() reads as follows:
I am afraid that one needs to make some assumptions about what sizes the key could be. You'll have to assume, for example, that you will only ever see 1024, 2048, or 4096-bit keys and then do something like:
This code can still be wrong on the (VERY rare) occasion, for example, when the first 1048 bits of a 2048 bit key are all 0. I think that is not something to worry about, though.
RSA 密钥的大小是其模数中的位数,因此您需要
myRSAKey.getModulus().bitLength()
。The size of an RSA key is the number of bits in its modulus, so you want
myRSAKey.getModulus().bitLength()
.我同意 Wheezil 在接受的答案中提到的担忧。具有 8 个前导 0 位的模数将破坏该方法。这种情况发生的概率为 0.4%,我认为这是不可接受的。所以我个人使用这个:
...因为 RSA 输出大小始终与密钥的模数相同,即使输入长度为 0 也是如此。
I share Wheezil's concerns mentioned in the accepted answer. A modulus with 8 leading 0-bits will break the approach. This has a 0.4% chance of occurring, which is unacceptable in my opinion. So I personally use this instead:
...since RSA output size is always the same size as the key's modulus, even with 0-length input.