How to encrypt & decrypt a file using asymmetric cryptography

Prosper Udofot
2 min readMar 1, 2024

--

Asymmetric cryptography,” also known as public-key cryptography, is a cryptographic technique that uses a pair of keys for encryption and decryption: a public key and a private key. Unlike symmetric cryptography, where the same key is used for both encryption and decryption, asymmetric cryptography uses different keys for these operations.

To encrypt and decrypt a file using asymmetric cryptography, I took the following steps:

Step 1: I generated a key pair, I generated a private key with 512 bits key length using the following command as depicted in the screenshot below:

openssl.exe genrsa -out private.key 512

Step 2: After generating a private key, then I generated a public key from my private key using the following command as depicted in the screenshot below:

openssl.exe rsa -in private.key -pubout -out public.key

Step 3: I used my public key to encrypt the file named wonderful.txt with the following command as depicted on the screenshot below:

openssl.exe rsautl -encrypt -inkey public.key -pubin -in wonderful.txt -out wonderful.encrypted

Step 4: After encrypting the filed named wonderful.txt, I named the encrypted file wonderful.encrypted, then I used my private key to decrypt the wonderful.encrypted file with the following command as depicted in the screenshot below:

Final step: After decrypting the file named wonderful. Decrypted, I also used the following command to display the content of the decrypted file in plaintext:

type wonderful.txt

Thanks for taking the time to read my article

--

--