如何在 Java 中获取 RSA 密钥的大小

发布于 2024-09-03 01:38:02 字数 53 浏览 7 评论 0原文

给定一个 java.security.interfaces.RSAKey,如何获取它的大小?

Given an java.security.interfaces.RSAKey, how do I get it's size?

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

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

发布评论

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

评论(4

自控 2024-09-10 01:38:02

你可以试试这个:

key.getModulus().bitLength();

You could try this:

key.getModulus().bitLength();
失与倦" 2024-09-10 01:38:02

(编辑:在我理解对 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() 内容如下:

返回此 BigInteger 的最小二进制补码表示形式的位数,不包括符号位。对于正 BigIntegers,这相当于普通二进制表示中的位数。 (计算 (ceil(log2(this < 0 ? -this : this+1)))。)

恐怕需要对密钥的大小做出一些假设。例如,您必须假设您只会看到 1024、2048 或 4096 位密钥,然后执行以下操作:

int keySize;
int bitLength = key.getModulus().bitLength();
if (bitLength <= 512) {
  throw new IllegalArgumentException(...)
}
else if (bitLength <= 1024) {
  keySize = 1024;
}
else if (bitLength <= 2048) {
  keySize = 2048;
}
else if (bitLength <= 4096) {
  keySize = 4096;
}
else {
  throw new IllegalArgumentException(...)
}
return keySize;

此代码在(非常罕见)场合仍然可能是错误的,例如,当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, then key.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:

Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit. For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation. (Computes (ceil(log2(this < 0 ? -this : this+1))).)

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:

int keySize;
int bitLength = key.getModulus().bitLength();
if (bitLength <= 512) {
  throw new IllegalArgumentException(...)
}
else if (bitLength <= 1024) {
  keySize = 1024;
}
else if (bitLength <= 2048) {
  keySize = 2048;
}
else if (bitLength <= 4096) {
  keySize = 4096;
}
else {
  throw new IllegalArgumentException(...)
}
return keySize;

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.

岛歌少女 2024-09-10 01:38:02

RSA 密钥的大小是其模数中的位数,因此您需要
myRSAKey.getModulus().bitLength()

The size of an RSA key is the number of bits in its modulus, so you want
myRSAKey.getModulus().bitLength().

神经暖 2024-09-10 01:38:02

我同意 Wheezil 在接受的答案中提到的担忧。具有 8 个前导 0 位的模数将破坏该方法。这种情况发生的概率为 0.4%,我认为这是不可接受的。所以我个人使用这个:

Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, yourKey);
int keyBitSize = rsa.getOutputSize(0) * Byte.SIZE;

...因为 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:

Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, yourKey);
int keyBitSize = rsa.getOutputSize(0) * Byte.SIZE;

...since RSA output size is always the same size as the key's modulus, even with 0-length input.

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