I would prefer to use the openssl
utility as it seems to be fairly ubiquitous.
Convert RSA public key and private key to PEM format:
$ openssl rsa -in ~/.ssh/id_rsa -outform pem > id_rsa.pem
$ openssl rsa -in ~/.ssh/id_rsa -pubout -outform pem > id_rsa.pub.pem
Encrypting a file with your public key:
$ openssl rsautl -encrypt -pubin -inkey id_rsa.pub.pem -in file.txt -out file.enc
Decrypting the file with your private key:
$ openssl rsautl -decrypt -inkey id_rsa.pem -in file.enc -out file.txt
But, as Gilles commented above this is only suitable for encrypting files smaller than your public key, so you could do something like this:
Generate a password, encrypt the file with it symmetrically, and encrypt the password with your public, key saving it to file:
$ openssl rand 64 |
tee >(openssl enc -aes-256-cbc -pass stdin -in file.txt -out file.enc) |
openssl rsautl -encrypt -pubin -inkey id_rsa.pub.pem -out file.enc.key
Decrypt the passphrase with your private key and use it to decrypt the file:
$ openssl rsautl -decrypt -inkey id_rsa.pem -in file.enc.key |
openssl enc -aes-256-cbc -pass stdin -d -in file.enc -out file.txt
You'll end up with two files, your encrypted file and your encrypted passphrase, but put into a script it would work nicely.
You could even add a tar cvf file file.enc file.enc.key
to tidy up.
Optimally, you would maximize the size of your passphrase as well changing rand 64
to the size of your public key.