How to use the public key cryptography

Here is the case:

1. 'A' will send the public key to 'B'. Now 'B' will encrypt a file (secret.txt) with public key of 'A'
2. 'A' will receive the encrypted (secret.txt) from 'B'
3. 'A' will decrypt the encrupted file using his private key

Appreciate if you could give the working example using openssl or any any command in linux.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave HoweSoftware and Hardware EngineerCommented:
That is accurate, however, openssl uses a more complex solution in practice.

A sends a certificate (Public key and other identifying info, digitally signed) to B
B symmetric encrypts text to A using a random key, then encrypts the key with the PK taken from the certificate
A decrypts the secret key using their private key, and uses the secret key to decrypt the message.

Now that we have that out of the way, Openssl *does* expose the primitives if you want them - it's a case of use at your own risk though. Here are some examples using rsa

to encrypt:
openssl rsautl -in <filename> -out <filename> -pubin -inkey <rsa-public-keyfile> -encrypt

to decrypt:
openssl rsautl -in <filename> -out <filename> -inkey <rsa-private-keyfile> -decrypt

and of course you will be wanting to generate the keys for the above functions:
openssl genpkey -algorithm RSA -out <rsa-private-keyfile> -pkeyopt rsa_keygen_bits:2048
openssl rsa -in <rsa-private-keyfile> -out <rsa-public-keyfile> -pubout

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.