生成具有特定公共指数的RSA密钥

huangapple go评论85阅读模式
英文:

Generating RSA keys with a specific public exponent

问题

crypto/rsa库提供了以下函数来生成新的RSA私钥:

func GenerateKey(random io.Reader, bits int) (*PrivateKey, error)

该函数似乎默认使用65537作为公共指数值。是否有一个API可以用来生成具有我选择的公共指数的RSA私钥,而不依赖于OpenSSL或其他C库?

英文:

The crypto/rsa library has the below function to generate a new RSA private key.

func GenerateKey(random io.Reader, bits int) (*PrivateKey, error)

This appears to default to use 65537 as the public exponent value. Is there an API I can use to generate a RSA private key with a public exponent of my choice that does not have a dependency on OpenSSL or another C library?

答案1

得分: 2

在Go语言中,你不会找到这样的API。

这是因为出于多种原因,3和65537满足RSA工作所需的要求,并且可以使RSA实现变得快速。

以下是这两个属性的解释:

  • 首先,为了使RSA工作,公共指数必须与整数集合的基数互质,该集合包括介于1和模数之间与模数互质的整数(请参阅RSA算法以了解原因)。因此,选择一个质数是有机会使该数字与上述描述的集合的基数互质的好方法,这就是为什么大多数情况下人们选择质数作为公共指数的原因(如果不是这种情况,他们会计算一个新的模数而不是更改公共指数)。由于上述描述的集合的基数是偶数,因此您不能选择2(如果模数是质数p和q的乘积,则该集合具有(p-1)(q-1)个元素,显然是一个偶数)。

  • 此外,为了进行快速计算,公共指数的二进制表示中设置为1的位数必须尽可能低。 2是唯一一个二进制表示中只有一个位设置为1的质数。由于不能选择2,您将选择一个质数,其二进制表示中只有2位设置为1。因此,这样一个质数与任何其他数字的算术乘积只需要一次加法和逻辑左移。

到目前为止,已知满足这些要求的整数只有3、5、17、257和65537。

英文:

You will not find such an API in Go.

This is because, for many reasons, 3 and 65537 match requirements needed for RSA to work and for RSA implementations to be fast.

Here are the explanations for these two properties:

  • First, for RSA to work, the public exponent must be coprime with the cardinal number of the set of integers, between 1 and the modulus, that are coprime with the modulus (see RSA algorithm to understand why). So, choosing a prime number is a good way to have a chance for this number to be coprime with the cardinal number of the set described above, this is why people choose prime numbers as public exponents, most of the time (and if it is not the case, they compute a new modulus instead of changing the public exponent). Since the cardinal number of the set described above is even, you can not choose 2 (if the modulus is the product of primes p and q, this set has (p-1)(q-1) elements, that is obviously an even number).

  • Moreover, for fast computation, the number of bits set to 1 in the binary representation of the public exponent must be lower as possible. 2 is the only prime number with one bit set to 1 in its binary representation. Since it can not be chosen, you will choose a prime number that only has 2 bits set to one in its binary representation. Therefore, the arithmetic product of such a prime with any other number only needs one addition and a logical shift left.

The only known integers (up to now) that match these requirements are 3, 5, 17, 257 and 65537.

huangapple
  • 本文由 发表于 2017年6月21日 10:36:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/44665958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定