Skip to content

Instantly share code, notes, and snippets.

@arbabnazar
Last active February 22, 2021 07:22
Show Gist options
  • Save arbabnazar/f34076eec8198358fc497947d8526fb3 to your computer and use it in GitHub Desktop.
Save arbabnazar/f34076eec8198358fc497947d8526fb3 to your computer and use it in GitHub Desktop.

Convert the public key into specify key format (PKCS8)

ssh-keygen -f ~/.ssh/id_rsa.pub -e -m PKCS8 > ~/.ssh/id_rsa.pem

Using the public PKCS8 file to encrypt a string

echo "sometext" | openssl rsautl -encrypt -pubin -inkey ~/.ssh/id_rsa.pem > ~/encrypted-file.txt

OR file

openssl rsautl -encrypt -inkey ~/.ssh/id_rsa.pem -pubin -in ~/secure-message-file.txt -out ~/encrypted-file.txt

To decrypt, you'll need the private key

openssl rsautl -decrypt -inkey ~/.ssh/id_rsa -in ~/encrypted-file.txt -out ~/decrypted-file.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment