我不太明白这个问题:
根据https://www.madboa.com/geek/openssl/#key-rsa,您可以从私钥生成公钥。
openssl genrsa -out mykey.pem 1024
openssl rsa -in mykey.pem -pubout > mykey.pub
我最初的想法是它们是成对产生的。
RSA私钥中是否包含该和?还是公钥?
我不太明白这个问题:
根据https://www.madboa.com/geek/openssl/#key-rsa,您可以从私钥生成公钥。
openssl genrsa -out mykey.pem 1024
openssl rsa -in mykey.pem -pubout > mykey.pub
我最初的想法是它们是成对产生的。
RSA私钥中是否包含该和?还是公钥?
当前回答
寻找SSH公钥的人…
如果您希望提取用于OpenSSH的公钥,则需要以稍微不同的方式获取公钥
$ ssh-keygen -y -f mykey.pem > mykey.pub
此公钥格式与OpenSSH兼容。将公钥追加到remote:~/。Ssh /authorized_keys,就可以开始了
从SSH-KEYGEN文档(1)
Ssh-keygen -y [-f input_keyfile]
-y该选项将读取私有的OpenSSH格式文件,并将OpenSSH公钥打印到stdout。
其他回答
在大多数生成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公钥信息的严格超集。
在这段代码中,我们首先创建的是RSA密钥,它是私有的,但它也有一对公钥,所以要获得实际的公钥,我们只需这样做
openssl rsa -in mykey.pem -pubout > mykey.pub
希望你得到更多的信息检查这个
公钥并不像某些人认为的那样存储在PEM文件中。私钥文件上的DER结构如下:
Openssl rsa -text -in mykey.pem
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
}
因此,有足够的数据来计算公钥(模量和公共指数),这就是openssl rsa -in mykey。Pem -pubout可以
我下面的回答有点长,但希望它能提供一些以前的回答中所缺少的细节。我将从一些相关的陈述开始,最后回答最初的问题。
要使用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 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.