OpenSSL:RSA中使用私钥加密并使用公钥解密

mon*_*ser 9 encryption rsa signature digital-signature

我想使用 OpenSSL 和 RSA 算法用私钥加密文件:

openssl rsautl -in txt.txt -out txt2.txt -inkey private.pem -encrypt
Run Code Online (Sandbox Code Playgroud)

现在如果我进行解密操作:

openssl rsautl -in txt2.txt -pubin -inkey public.pem -decrypt
Run Code Online (Sandbox Code Playgroud)

此操作需要私钥

我知道我应该使用公钥来加密,如果我使用私钥,我就会得到签名。

但是,我想这样做是为了学习。

kel*_*aka 4

您错误地使用了密钥。在公钥加密中,加密使用公钥:

openssl rsautl -in txt.txt -out txt2.txt -inkey public.pem -pubin -encrypt
Run Code Online (Sandbox Code Playgroud)

为了解密,使用与公钥相关的私钥:

openssl rsautl -in txt2.txt -inkey private.pem -decrypt
Run Code Online (Sandbox Code Playgroud)

私钥(不带 -pubin)可用于加密,因为它实际上包含公共指数。请注意,RSA 通常不应直接用于加密数据,而只能用于“封装”(RSA-KEM) 或“包装”用于对称加密的密钥。

但你提到你实际上想学习签名。尽管历史上 RSA 签名有时被描述为“使用私钥加密”,但该描述具有误导性,并且实际实施被发现是不安全的。签名和验证实际上是与加密和解密不同的不同操作,并且rsautl仅执行其中的一部分。例如,您可以执行以下操作:

# hash the data and encode the result in ASN.1 
openssl rsautl -sign -in hashenc.dat -out sig.dat -inkey private.pem
...
# on the recipient (with signature and purportedly correct data)
openssl rsautl -verify -in sig.dat -out hashenc.dat -inkey public.pem -pubin 
# or often more appropriate use a certificate for the public key
openssl rsautl -verify -in sig.dat -out hashenc.dat -inkey cert.pem -certin
# now either decode hashenc.dat and compare the hash
# to a new hash of the data (which should be the same)
# or compare all of hashenc.dat to an encoding of a new hash
Run Code Online (Sandbox Code Playgroud)

相反,最好使用它执行PKCS1 指定的openssl dgst整个签名和验证序列,例如 rfc8017。例如,对于使用 SHA256 的RSASSA-PKCS1v1_5 签名:

openssl dgst -sha256 -sign private.pem -in data.txt -out sig.dat
# or can be abbreviated
openssl sha256 -sign private.pem -in data.txt -out sig.dat
# hashes the data, encodes the hash, does type 1 padding and modexp d
...
openssl dgst -sha256 -verify public.pem -in data.txt -signature     sig.dat
# or abbreviated 
openssl sha256 -verify public.pem -in data.txt -signature sig.dat 
# does modexp e and type 1 unpadding, and compares the result to a hash of the data

# notice you don't specify which key is public or private
# because this command knows what to expect

# however it does not accept the public key from a certificate, 
# you must extract the public key from the cert first
Run Code Online (Sandbox Code Playgroud)

这种形式(但不是rsautl)还支持更新的、技术上更好的、但没有广泛使用的 PSS 填充。这仅在dgst手册页上引用,并且大部分记录在pkeyutl手册页上,这并不完全明显。

在其他更切题的堆栈上,请参阅:https: //security.stackexchange.com/questions/93603/understanding-digitial-certifications
https://security.stackexchange.com/questions/87325/if-the -public-key-cant-be-used-for-decrypting
https://security.stackexchange.com/questions/11879/is-encrypting-data-with-a-private-key-dangerous
https://security.stackexchange .com/questions/68822/trying-to-understand-rsa-and-its-terminology
https://crypto.stackexchange.com/questions/2123/rsa-encryption-with-private-key-and-decryption-with- a-公钥
https://crypto.stackexchange.com/questions/15997/is-rsa-encryption-the-same-as-signature- Generation
https://crypto.stackexchange.com/questions/15295/why-签署小数据之前需要进行哈希处理