I'm looking for a way to perform encrypted nightly backups of a server I administer, without storing the decryption key on the server.

A form of PK encryption seems ideal for this, as I can store the public key on the server, and the private key somewhere else private (like on a post-it attached to my monitor, or for real security, under my matress). If I need to restore from backups, I can get the private key out of hiding. At least, so goes the theory...

However, only a small amount of data can be encrypted with an RSA public key, so this needs to be a symmetric key used for the actual encryption, which must be generated at the time of file encryption and stored alongside the backup so I don't have to store this on the server.

Given the above, do the following two simple shell scripts make security sense, and what (if any) existing and proven Open-Source software is available to do something like this?

#this is the encryption system used like this: ./crypt.sh publickey.pem mycleartextfile.txt


SESSION_KEY=`openssl rand 256`
echo "$SESSION_KEY" | openssl pkeyutl -encrypt -pubin -inkey "$PUBKEY" -out "$CLEARTEXT_FILE".key
echo "$SESSION_KEY" | openssl enc -in "$CLEARTEXT_FILE" -e -blowfish -out "$CLEARTEXT_FILE".openssl -pass stdin

...and the decryption script:

#usage ./decrypt privatekey.pem encrypted-file-session.key encrypted-file

openssl pkeyutl -decrypt -inkey "$PRIVATE_KEY" -in "$SESSION_KEY_CRYPT" \
| openssl enc -d -blowfish -in "$CIPHERTEXT_FILE" -out "$CIPHERTEXT_FILE".out -pass stdin

Any thoughts on this are much appreciated.

  • $\begingroup$ You've got the right idea. Note: openssl rand 256 genertes 256 bytes of output. 256 bits would be enough for a symmetric key. $\endgroup$ – Brock Hansen Feb 28 '14 at 23:32
  • 2
    $\begingroup$ openssl rand 256's output is binary. How sure are you that your SESSION_KEY variable can hold 256 bytes that may include binary zero bytes? A test script I just tried couldn't, under a Linux bash shell. $\endgroup$ – Brock Hansen Feb 28 '14 at 23:34
  • $\begingroup$ @BrockHansen I hadn't considered that, but I tried a bunch of times in GNU bash, version 4.2.45(1)-release on Debian Sid and it seems to work fine. I will however look into this concern. Thanks for pointing it out. $\endgroup$ – user12265 Feb 28 '14 at 23:36
  • 2
    $\begingroup$ Why blowfish? Doesn't it have a 64-bit block size? Why not AES? $\endgroup$ – Brock Hansen Feb 28 '14 at 23:41
  • 1
    $\begingroup$ Why not just use GnuGPG ? Don't roll your own encryption protocol. $\endgroup$ – DrEntropy Jan 24 '16 at 19:31