1
$\begingroup$

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?

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

PUBKEY=$1
CLEARTEXT_FILE=$2

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:

#!/bin/bash
#usage ./decrypt privatekey.pem encrypted-file-session.key encrypted-file
PRIVATE_KEY=$1
SESSION_KEY_CRYPT=$2
CIPHERTEXT_FILE=$3

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.

$\endgroup$

closed as off-topic by e-sushi Jul 5 '17 at 2:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions are off-topic even if you are writing or debugging cryptographic code. Unless your question is specifically about how the cryptographic algorithm or protocol works, you should look into asking on Stack Overflow instead." – e-sushi
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\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$ – ldrumm 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