Encrypt with GPG using a key passed as CLI argument

Use one of the --passphrase-... options, in batch mode:

  • --passphrase-fd reads the passphrase from the given file descriptor

      echo mysuperpassphrase | gpg --batch -c --passphrase-fd 0 file
    
  • --passphrase-file reads the passphrase from the given file

      echo mysuperpassphrase > passphrase
      gpg --batch -c --passphrase-file passphrase file
    
  • --passphrase uses the given string

      gpg --batch -c --passphrase mysuperpassphrase file
    

These will all encrypt file (into file.gpg) using mysuperpassphrase.

With GPG 2.1 or later, you also need to set the PIN entry mode to “loopback”:

gpg --batch -c --pinentry-mode loopback --passphrase-file passphrase file

etc.

Decryption can be performed in a similar fashion, using -d instead of -c, and redirecting the output:

gpg --batch -d --passphrase-file passphrase file.gpg > file

etc.