OpenSSL: Asymmetric en- and decryption of a file


Hi,

this post describes the en- and decryption of a file with a asymmetric encryption algorithm.

Asymmetric encryption means you encrypt data by a public key and can only decrypt this data with a private key associated with the public key.

An example. Create a file to encrypt

michael@debdev ~ # echo "My Secret Data" > file.txt

Make a key pair

michael@debdev ~ # openssl genpkey -algorithm RSA -out myPrivate.key -pkeyopt rsa_keygen_bits:2048

List all available public key algorithms

michael@debdev ~ # openssl list-public-key-algorithms
Name: OpenSSL RSA method
        Type: Builtin Algorithm
        OID: rsaEncryption
        PEM string: RSA
Name: rsa
        Type: Alias to rsaEncryption
Name: OpenSSL PKCS#3 DH method
        Type: Builtin Algorithm
        OID: dhKeyAgreement
        PEM string: DH
...

Generate the associated public key

michael@debdev ~ # openssl rsa -pubout -in myPrivate.key -out publicKey.pem

encrypt the file with the public key

michael@debdev ~ # openssl rsautl -encrypt -inkey publicKey.pem -pubin -in file.txt -out encypted_file.txt

and decrypt the file with the private key

michael@debdev ~ # openssl rsautl -decrypt -inkey myPrivate.key -in encypted_file.txt -out clear_text_file.txt

Michael

2 thoughts on “OpenSSL: Asymmetric en- and decryption of a file”

Leave a Reply Cancel reply