c2h*_*2h2 368 openssl rsa public-key-encryption
我真的不明白这个:
根据:http://www.madboa.com/geek/openssl/#key-rsa,您可以从私钥生成公钥.
openssl genrsa -out mykey.pem 1024
openssl rsa -in mykey.pem -pubout > mykey.pub
Run Code Online (Sandbox Code Playgroud)
我最初的想法是,它们是一起生成的.RSA私钥是否包含总和?还是公钥?
Raa*_*aam 529
openssl genrsa -out mykey.pem 1024
Run Code Online (Sandbox Code Playgroud)
实际上会产生一个公钥 - 私钥对.该对存储在生成的mykey.pem文件中.
openssl rsa -in mykey.pem -pubout > mykey.pub
Run Code Online (Sandbox Code Playgroud)
将提取公钥并将其打印出来.以下是指向更好地描述此内容的页面的链接.
编辑:在这里查看示例部分.要输出私钥的公共部分:
openssl rsa -in key.pem -pubout -out pubkey.pem
Run Code Online (Sandbox Code Playgroud)
Tha*_*you 266
如果您要提取用于OpenSSH的公钥,则需要以不同方式获取公钥
$ ssh-keygen -y -f mykey.pem > mykey.pub
Run Code Online (Sandbox Code Playgroud)
此公钥格式与OpenSSH兼容.附加公钥remote:~/.ssh/authorized_keys,你会很高兴
来自的文档 SSH-KEYGEN(1)
Run Code Online (Sandbox Code Playgroud)ssh-keygen -y [-f input_keyfile]-y此选项将读取私有的OpenSSH格式文件,并将OpenSSH公钥打印到stdout.
Jam*_*olk 67
在大多数生成RSA私钥的软件中,包括openssl,私钥表示为PKCS#1 RSAPrivatekey对象或其某些变体:
A.1.2 RSA私钥语法
RSA私钥应使用ASN.1类型
RSAPrivateKey表示:Run Code Online (Sandbox Code Playgroud)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公钥中信息的严格超集.
gol*_*lem 25
我在下面的回答有点冗长,但希望它提供了以前答案中缺少的一些细节.我将从一些相关的陈述开始,最后回答最初的问题.
要使用RSA算法加密某些内容,您需要模数和加密(公共)指数对(n,e).那是你的公钥.要使用RSA算法解密某些内容,您需要模数和解密(私有)指数对(n,d).那是你的私钥.
要使用RSA公钥加密某些内容,您可以将明文视为数字并将其提高到e模数n的幂:
ciphertext = ( plaintext^e ) mod n
Run Code Online (Sandbox Code Playgroud)
要使用RSA私钥解密某些内容,您需要将密文视为数字并将其提升为d模数n的幂:
plaintext = ( ciphertext^d ) mod n
Run Code Online (Sandbox Code Playgroud)
要使用openssl生成私有(d,n)密钥,您可以使用以下命令:
openssl genrsa -out private.pem 1024
Run Code Online (Sandbox Code Playgroud)
要使用openssl从私钥生成公共(e,n)密钥,您可以使用以下命令:
openssl rsa -in private.pem -out public.pem -pubout
Run Code Online (Sandbox Code Playgroud)
要剖析由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
Run Code Online (Sandbox Code Playgroud)
私钥不应该只包含(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
Run Code Online (Sandbox Code Playgroud)
PKCS私有密钥的这种结构由PKCS#1 v1.5推荐作为替代(第二)表示.PKCS#1 v2.0标准完全排除了替代表示中的e和d指数.PKCS#1 v2.1和v2.2通过可选地包括更多与CRT相关的组件,提出对替代表示的进一步改变.
要查看public.pem公共RSA密钥的内容,请运行以下命令(输出截断为此处的标签):
openssl rsa -in public.pem -text -pubin -noout
Modulus - n
Exponent (public) - e
Run Code Online (Sandbox Code Playgroud)
这里没有惊喜.正如所承诺的那样,这只是(n,e)对.
现在终于回答了最初的问题:如上所示,使用openssl生成的私有RSA密钥包含公钥和私钥的组件等等.当您从私钥生成/提取/派生公钥时,openssl会将其中两个组件(e,n)复制到一个单独的文件中,该文件将成为您的公钥.
Uxi*_*xio 21
有些人认为公钥不存储在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
}
Run Code Online (Sandbox Code Playgroud)
所以,有足够的数据来计算公钥(模数和公共指数),这是什么openssl rsa -in mykey.pem -pubout呢
在这个代码中首先我们创建RSA密钥,这是私有的,但它也有一对公钥,所以为了得到你的实际公钥我们只是这样做
openssl rsa -in mykey.pem -pubout > mykey.pub
Run Code Online (Sandbox Code Playgroud)
希望你得到更多信息检查这一点
首先,快速回顾一下RSA密钥生成。
公用密钥由模数和公用指数组成。
最小私钥将由模数和私钥指数组成。从已知的模数和私有指数到相应的公共指数,没有从计算上可行的保证方法。
然而:
因此,在大多数实际的RSA实现中,您可以从私钥中获取公钥。在这不可能的情况下,有可能构建基于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
Run Code Online (Sandbox Code Playgroud)
您将看到该私钥文件包含素数以及所有其他信息,而公共文件仅包含模数和公共指数。