我不太明白这个问题:

根据https://www.madboa.com/geek/openssl/#key-rsa,您可以从私钥生成公钥。

openssl genrsa -out mykey.pem 1024
openssl rsa -in mykey.pem -pubout > mykey.pub

我最初的想法是它们是成对产生的。

RSA私钥中是否包含该和?还是公钥?


当前回答

使用以下命令:

openssl req -x509 -nodes -days 365 -sha256 -newkey rsa:2048 -keyout mycert.pem -out mycert.pem Loading 'screen' into random state - done Generating a 2048 bit RSA private key .............+++ ..................................................................................................................................................................+++ writing new private key to 'mycert.pem' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. If you check there will be a file created by the name : mycert.pem openssl rsa -in mycert.pem -pubout > mykey.txt writing RSA key If you check the same file location a new public key mykey.txt has been created.

其他回答

使用以下命令:

openssl req -x509 -nodes -days 365 -sha256 -newkey rsa:2048 -keyout mycert.pem -out mycert.pem Loading 'screen' into random state - done Generating a 2048 bit RSA private key .............+++ ..................................................................................................................................................................+++ writing new private key to 'mycert.pem' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. If you check there will be a file created by the name : mycert.pem openssl rsa -in mycert.pem -pubout > mykey.txt writing RSA key If you check the same file location a new public key mykey.txt has been created.

我下面的回答有点长,但希望它能提供一些以前的回答中所缺少的细节。我将从一些相关的陈述开始,最后回答最初的问题。

要使用RSA算法加密东西,你需要模和加密(公共)指数对(n, e),这是你的公钥。要用RSA算法解密你需要模和解密(私有)指数对(n, d)这是你的私钥。

要使用RSA公钥加密某些内容,您可以将明文作为一个数字,并将其提高到e模n的幂:

ciphertext = ( plaintext^e ) mod n

要使用RSA私钥解密某些内容,您可以将密文视为一个数字,并将其提高到d模n的幂:

plaintext = ( ciphertext^d ) mod n

使用openssl生成私钥(d,n)可以使用以下命令:

openssl genrsa -out private.pem 1024

使用openssl从私钥生成公钥(e,n),可以使用以下命令:

openssl rsa -in private.pem -out public.pem -pubout

来剖析私人的内容。pem由openssl命令生成的RSA私钥,执行以下命令(这里的输出被截断为标签):

openssl rsa -in private.pem -text -noout | less

modulus         - n
privateExponent - d
publicExponent  - e
prime1          - p
prime2          - q
exponent1       - d mod (p-1)
exponent2       - d mod (q-1)
coefficient     - (q^-1) mod p

Shouldn't private key consist of (n, d) pair only? Why are there 6 extra components? It contains e (public exponent) so that public RSA key can be generated/extracted/derived from the private.pem private RSA key. The rest 5 components are there to speed up the decryption process. It turns out that by pre-computing and storing those 5 values it is possible to speed the RSA decryption by the factor of 4. Decryption will work without those 5 components, but it can be done faster if you have them handy. The speeding up algorithm is based on the Chinese Remainder Theorem.

是的,私人。pem RSA私钥实际上包含这8个值;在运行上一个命令时,它们都不会动态生成。尝试运行以下命令并比较输出结果:

# Convert the key from PEM to DER (binary) format
openssl rsa -in private.pem -outform der -out private.der

# Print private.der private key contents as binary stream
xxd -p private.der

# Now compare the output of the above command with output 
# of the earlier openssl command that outputs private key
# components. If you stare at both outputs long enough
# you should be able to confirm that all components are
# indeed lurking somewhere in the binary stream
openssl rsa -in private.pem -text -noout | less

这种RSA私钥结构被PKCS#1 v1.5推荐为一种替代(第二种)表示。PKCS#1 v2.0标准将e和d指数完全排除在替代表示中。PKCS#1 v2.1和v2.2通过可选地包括更多与crt相关的组件,提出了对替代表示的进一步更改。

公开查看内容。pem public RSA key执行如下命令(此处输出截断为标签):

openssl rsa -in public.pem -text -pubin -noout

Modulus             - n
Exponent (public)   - e

这里没有惊喜。就像之前说的,是(n, e)对。

现在终于回答了最初的问题:如上所示,使用openssl生成的私有RSA密钥包含公钥和私钥的组件,以及其他一些组件。当您从私钥生成/提取/派生公钥时,openssl将其中两个组件(e,n)复制到一个单独的文件中,该文件将成为您的公钥。

openssl genrsa -out mykey.pem 1024

实际上会产生一个公私钥对。该对存储在生成的mykey中。pem文件。

openssl rsa -in mykey.pem -pubout > mykey.pub

将提取公钥并打印出来。这里有一个页面的链接,可以更好地描述这一点。

编辑:在这里检查示例部分。只输出私钥的公开部分:

openssl rsa -in key.pem -pubout -out pubkey.pem

使用SSH -keygen获取SSH使用的公钥:

ssh-keygen -y -f key.pem > key.pub

在大多数生成RSA私钥的软件中,包括OpenSSL,私钥被表示为PKCS#1 RSAPrivatekey对象或其变体:

A.1.2 RSA private key syntax An RSA private key should be represented with the ASN.1 type RSAPrivateKey: RSAPrivateKey ::= SEQUENCE { version Version, modulus INTEGER, -- n publicExponent INTEGER, -- e privateExponent INTEGER, -- d prime1 INTEGER, -- p prime2 INTEGER, -- q exponent1 INTEGER, -- d mod (p-1) exponent2 INTEGER, -- d mod (q-1) coefficient INTEGER, -- (inverse of q) mod p otherPrimeInfos OtherPrimeInfos OPTIONAL }

正如您所看到的,这种格式有许多字段,包括模数和公共指数,因此是RSA公钥信息的严格超集。

被称为“私钥”的文件包含的信息比单独的私钥多得多,它包括生成私钥/公钥对所需的所有数据(质数、模数、指数等)。

很容易看到这些信息:

openssl genrsa -out private.pem 1024   #generate private key file
openssl rsa -in private.pem -text      #view info in the private key file
openssl rsa -in private.pem -pubout -out public.pem  #extract public key to file
openssl rsa -in public.pem -pubin -text  #view info in the public key file

您将看到私钥文件包括质数和所有其他信息,而公共文件只包括模数和公共指数。