RSA 仅使用 openssl 的私钥加密和解密,对吗?

Is RSA Encrypt & Decrypt only with Private Key by openssl, correct?

这是我的测试用例。

$ openssl genrsa -out private.pem 2048
$ openssl rsa -in private.pem -outform PEM -pubout -out public.pem  # but I don't use it.

$ touch raw_data.log && echo 123456 >> raw_data.log
$ openssl rsautl -encrypt -in raw_data.log -inkey private.pem > enc.raw_data.log
$ openssl rsautl -decrypt -in enc.raw_data.log -inkey private.pem > dec.raw_data.log

$ cat raw_data.log
$ cat dec.raw_data.log

为什么我只能使用 rsa 私钥加密和解密数据。(不是 public 密钥来加密数据)

是否正确?

如果你读过man page for openssl rsautl,你会发现你可以使用pubin选项来使用public密钥

加密

-inkey file the input key file, by default it should be an RSA private key.

-pubin the input file is an RSA public key.

因此您可以使用私钥(默认)或 public 密钥(使用 pubin 选项)进行加密

openssl rsautl -encrypt -inkey pubkey.pem -pubin -in raw_data.log -out enc.raw_data.log