2

I would like to encrypt many files using symmetric encryption such as AES256, using a single memorised password.

I was planning to use gpg but gpg symmetric encryption does not work with --multifile flag. gpg has the ability to cache the passphrase during decryption, but I couldn't find this option for encryption.

What is the best way of doing this?

Conditions:

  • Prefer using popular tools that have stood test of time in public. Ideally I would only use tools already shipped with linux
  • It won't work for me to create a giant tarball and encrypt it. I need each file to be encrypted separately.
  • I want to type the passphrase only once, not once per file.

Some solutions that seem to have drawbacks:

  • use openssl cli not pgp - may have vulnerabilities in implementation of cryptography as claimed by users here
  • use passphrase flag in gpg and store the passphrase in a file - not recommended as other people may have access to the computer in future and securely erasing the disk is non-trivial.
  • use this script - I don't know if this code has bugs or can be accidentally misused.
  • use pgp asymmetric encryption and export the encrypted private key. Everyone who has access to encrypted files will also have access to the encrypted private key. - This might be slightly slower but that is not an issue. It feels wrong, but this solution will work for me. I will consider this as worst case option.
  • use 7zip (or a similar tool) - maintained only by one company and may be less secure or less portable compared to pgp or openssl. I will consider this as worst case option.
11
  • 1
    WRT the vulnerabilities in openssl cli - are you referring to older versions of openssl enc that derived the AES key from the password using a single round of MD5 hashing? If so, this is no longer an issue in version 1.1.1 or newer. See security.stackexchange.com/questions/31492/… for more info. Or, better yet, use -K to specify an AES key directly, instead of a password, and avoid key derivation altogether.
    – mti2935
    Commented Sep 29 at 13:50
  • If that's acceptable to you, then you might want to consider combining find with openssl enc to encrypt multiple files in the manner that you described, where each file that find finds is encrypted separately. See unix.stackexchange.com/questions/207178/… for more info.
    – mti2935
    Commented Sep 29 at 13:53
  • 1
    This sounds like a scripting question.
    – schroeder
    Commented Sep 29 at 14:37
  • 1
    What's wrong with the passphrase file? Just shred the file once you have encrypted everything? Is a requirement that you want to encrypt and decrypt arbitrary files at arbitrary times?
    – schroeder
    Commented Sep 29 at 14:39
  • 1
    What's your threat model? What's wrong with the straightforward way of doing this? What's wrong with unsetting HISTFILE and assigning the password to a shell variable, or hardcoding it in a for loop? or working in tmpfs mounted dir? Or using the right permissions on the files you need? What can "other apps" do? It's so disarming seeing all these questions of people wanting to do all kinds of XY-problem weird stuff while lacking the basics of infosec. And it's even more disarming when people write generic, all-purpose, answers to farm a few unicorn points... Commented Sep 29 at 14:47

2 Answers 2

1

Conclusion: I just decided to use asymmetric encryption and export the (seedphrase-encrypted) private key so it is stored with the encrypted files.

I probably don't need to hunt for a complicated solution when the simple solution will work. If anyone here recommends against this, please let me know.

0

The following bash script fulfills your requirements:

#!/usr/bin/env bash

# Print usage information of the script
if (($# == 0)) || [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
  echo "Usage: $0 <filename> [<filename> ...]"
  exit 1
fi

echo "Passphrase:"
# Disable output to prevent terminal leakage of passphrase when the passphrase is entered
stty -echo
# Read passphrase via standard input
# The "-r" parameter of read prevents the interpretation of escape sequences
# Setting the IFS (Internal Field Separator) environment variable empty to allow the passphrase to contain whitespaces
IFS="" read -r passphrase
# Enable output
stty echo

# Execute GnuPG to encrypt every file with AES-256 given as a parameter to the script
for arg in "$@"; do
  gpg --batch --symmetric --passphrase-fd 0 --cipher-algo AES256 "$arg" <<< "$passphrase"
done
  • Save it as enc.sh and make it executable with chmod +x enc.sh.
  • To encrypt multiple files, execute ./enc.sh file1 file2 ... and enter your passphrase which is required once.
  • Files are encrypted with GnuPG using AES-256.
  • The passphrase will not be leaked through the terminal, the shell history or /proc's cmdline.
  • To decrypt a file execute gpg filename.gpg or use gpg --multifile --decrypt file1.gpg file2.gpg ... to decrypt multiple files at once.
12
  • 2
    This isn't really a programming community (like Stack Overflow) nor a code repository. I think it's more important to explain the concepts than to just dump a bunch of code and essential say "Just run this". Also note that the OP never asked for code or indicated that this is what they're struggling with -- they specifically asked about the right approach.
    – Ja1024
    Commented Sep 30 at 1:53
  • 1
    The OP also posted a link to a very similar script already, so I don't see how a second script helps here.
    – Ja1024
    Commented Sep 30 at 2:02
  • 1
    @security_paranoid Thank you for reminding, feel free to criticize the script. I have checked the script with shellcheck for obvious errors and tested it for proc leaks. I am not sure if I always have to be explicit about any warranties here since non-code answers may be as dangerous as answers containing code.
    – Marl Joos
    Commented Sep 30 at 3:47
  • 1
    @MarlJoos Thanks for replying! Your answer is super helpful. I decided to not go with it though, and just use asymmetric encryption instead (and export the seed phrase-encrypted private key). But someone else might find your answer helpful. Commented Sep 30 at 6:16
  • 1
    @MarlJoos: In the help center, there’s a section on the scope of this site. As you can see, it’s not “anything related to security”. For programming/scripting, there’s a separate community which specializes on exactly that: Stack Overflow. This is why dumping a bunch of code is usually not perceived as a good answer. I realize you’ve added comments, but still: Why not focus on the concepts? When you simply suggest reading the passphrase from standard input, then it’s already clear what you mean – no need for the exact Bash implementation.
    – Ja1024
    Commented Sep 30 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .