How to encrypt/decrypt files with OpenSSL.
To check the version of your OpenSSL
# command to check openssl version
openssl version
Generate private key.
# generate private key based on AES128 encryption with 1024 bit
openssl genrsa -aes128 -out private.pem 1024
# then type your password
# and then type your password again to confirm
To remove the pass phrase from the private key.
# command to remove the pass phrase
# skip this step if you do not wish to remove the pass phrase
openssl rsa -in private.pem -out private.pem
Extract the public key from the private key.
# command to read the private key, extract the public key and write to file
openssl rsa -in private.pem -pubout > public.pem
Let’s create a plain text file to encrypt.
# command to write 'hello world' to file
echo 'hello world' > plain_text.txt
You can now distribute this public key to anyone who wishes to send you an encrypted file. Encrypting the file.
# command to encrypt the plain text file
openssl pkeyutl -encrypt -inkey public.pem -pubin -in plain_text.txt -out encrypted_text.enc
Decrypting the file.
# command to decrypt the encrypted file
openssl pkeyutl -decrypt -inkey private.pem -in encrypted_text.enc > decrypted_text.txt