Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrypt a GPG file using a batch file

I am decrypting a gpg file using a batch file with the below code.

gpg.exe --output test.csv --batch --passphrase-fd 0 --decrypt WSB330TJ.CSTDJIDF.TXT.asc.14.04.22_00.59.gpg

Although it does Decrypt the file but I have to enter the passphrase manually. How can I improve it so that it automatically pick the passphrase and decrypt the file without any manual intervention? What should I add here?

like image 480
user3219897 Avatar asked Dec 19 '22 15:12

user3219897


1 Answers

You tell GnuPG to read the passphrase from stdin by using --passphrase-fd 0. There are different options to read the passphrase, from man gpg:

   --passphrase-fd n
          Read the passphrase from file descriptor n.  Only  the  first  line
          will  be  read  from  file  descriptor  n.  If you use 0 for n, the
          passphrase will be read from STDIN. This can only be used  if  only
          one passphrase is supplied.

   --passphrase-file file
          Read  the  passphrase  from  file file. Only the first line will be
          read from file file. This can only be used if only  one  passphrase
          is  supplied.  Obviously, a passphrase stored in a file is of ques-
          tionable security if other users can read this file. Don't use this
          option if you can avoid it.

   --passphrase string
          Use  string  as  the  passphrase. This can only be used if only one
          passphrase is supplied. Obviously, this  is  of  very  questionable
          security  on  a multi-user system. Don't use this option if you can
          avoid it.

If you use GnuPG 2, remember to use --batch, otherwise the passphrase options will be ignored.

If you stored the passphrase in a file, use --passphrase-file password.txt, if you want to pass it as a string use --passphrase "f00b4r" (both times using appropriate parameter values, of course).

@Thierry noted in the comments that (especially when using Windows) make sure to end the file with a UNIX line feed (\n / LN) instead of a Windows line feed + carriage return (\n\r / LNRF).

like image 125
Jens Erat Avatar answered Jan 25 '23 00:01

Jens Erat