英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论