Create a file:
echo ‘This is a sekret’ >/tmp/msg.txt

Export public key:
openssl rsa -in ~/private.pem -out /tmp/public.pub -outform PEM -pubout

Encrypt file with public key (anyone can have this key):
openssl rsautl -encrypt -inkey /tmp/public.pub -pubin -in /tmp/msg.txt -out /tmp/file.enc

Decrypt the file with private key (only you should have the private key):
openssl rsautl -decrypt -inkey ~/private.pem -in /tmp/file.enc -out /tmp/decrypted.txt

Check decoded message:
cat /tmp/decrypted.txt

By Jon

One thought on “Use your ssh key to encrypt / decrypt files”
  1. You can also use this code
    rsautl -encrypt -inkey /tmp/public.pub -pubin -in /tmp/msg.txt -out /tmp/file.enc to encrypt the file and decrypt using this code
    openssl rsautl -decrypt -inkey ~/private.pem -in /tmp/file.enc -out /tmp/decrypted.txt .

    Thanks
    Silvester Norman

    Change Mac Address

Leave a Reply