I see that you "want to do that for studying purposes." Then, for anyone else looking at this answer, it shall too be used for "studying purposes," and not for any production or purportedly secure system.
As @kelalaka points out, in public-key cryptography, the public key is used for encryption, and the private key is used for decryption. However, if you are studying RSA, you also know that "encryption/decryption" and "signature/verification" use the same underlying mathematical formula:

Further, encryption raises m
to the power of e
, mod(n
) and decryption raises c
to the power of d
, mod(n
), where (e, n)
are the public key and (d, n)
are the private key. Given the mathematical basis, I think a natural question becomes, how can we raise m
to the power of d
instead, and vice versa?
It turns out that message signing raises hash(m)
to the power of d
, mod(n
), and message verification raises tag
to the power of e
, mod(n
). If you loosely define "encrypt" as "raise m
to the power of a number, mod (n
)", then Openssl CLI can "encrypt a file with the private key" (actually, sign to create a tag
). The rsautl
subcommand can be used like so (Note that the input data must be of the same bit-length as the key used for encrypting/signing, hence the 501 bytes of 'A' padding):
# Create the file to have a `tag` created for:
python3 -c "print('Hello World'+501*'A', end='')" > txt.txt
# Create a `tag` called `txt2.txt` for the `txt.txt` file:
openssl pkeyutl -sign -inkey private_key.pem -in tx.txt -out txt2.txt -pkeyopt rsa_padding_mode:none
Now txt2.txt
contains the result of raising txt.txt
to the power of d
, mod(n
).
If you loosely define "decrypt" as "raise m
to the power of a number, mod (n
)", then Openssl CLI can "decrypt a file with the public key" (actually, verify but see the raw results of an intermediate step). The rsautl
subcommand can be used like so:
openssl rsautl -verify -inkey public_key.pem -pubin -in txt2.txt -raw -hexdump
Now stdout
contains the result of raising txt2.txt
to the power of e
, mod(n
).