使用RSA私钥生成公钥?

帅比萌擦擦* 提交于 2020-02-27 23:49:56

我真的不明白这个:

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

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

我最初的想法是,它们是一起生成的。 RSA私钥是否包含总和? 还是公钥?


#1楼

在这个代码中首先我们创建RSA密钥,这是私有的,但它也有一对公钥,所以为了得到你的实际公钥我们只是这样做

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

希望你得到更多信息检查这一点


#2楼

人们在寻找SSH公钥......

如果您要提取用于OpenSSH的公钥,则需要以不同方式获取公钥

$ ssh-keygen -y -f mykey.pem > mykey.pub

此公钥格式与OpenSSH兼容。 将公钥附加到remote:~/.ssh/authorized_keys ,你就可以了


来自SSH-KEYGEN(1)文档SSH-KEYGEN(1)

ssh-keygen -y [-f input_keyfile]

-y此选项将读取私有的OpenSSH格式文件,并将OpenSSH公钥打印到stdout。


#3楼

有些人认为公钥不存储在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所做的openssl rsa -in mykey.pem -pubout


#4楼

Use the following commands:

1. 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.

2. If you check there will be a file created by the name : mycert.pem

3. openssl rsa -in mycert.pem -pubout > mykey.txt
writing RSA key

4. If you check the same file location a new public key : mykey.txt will be created.

#5楼

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

要使用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

要剖析由openssl命令生成的private.pem私有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

私钥不应该只包含(n,d)对吗? 为什么还有6个额外组件? 它包含e(公共指数),以便可以从private.pem私有RSA密钥生成/提取/派生公共RSA密钥。 其余5个组件用于加速解密过程。 事实证明,通过预先计算和存储这5个值,可以将RSA解密速度提高4倍。解密将在没有这5个组件的情况下工作,但如果你有方便的话,它可以更快地完成。 加速算法基于中国剩余定理

是的,private.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

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

要查看public.pem公共RSA密钥的内容,请运行以下命令(输出截断为此处的标签):

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

Modulus             - n
Exponent (public)   - e

这里没有惊喜。 正如所承诺的那样,这只是(n,e)对。

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!