Author Topic: File decryption using GPG command line  (Read 23462 times)

MW4

  • Full Member
  • ***
  • Posts: 143
File decryption using GPG command line
« on: January 29, 2014, 11:17:43 am »
Hi, has anyone written a batch script to decrypt a file using gpg?
The passphrase will be the same perpetually, so I just need it set up once.
The issue is passing the passphrase.



Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: File decryption using GPG command line
« Reply #1 on: January 29, 2014, 11:36:04 am »
I am not personally familiar with GPG. However maybe you can give this a try:

Code: Winbatch
;Sample Commandline
;gpg2.exe --passphrase "THISISTHEPASSPHRASE" -o "C:\OUTPUTFILENAM"E -d "C:\FILETODECRYPT.GPG"
DirChange('c:\tools\')
progname = 'c:\tools\gpg2.exe'
params = '--passphrase "THISISTHEPASSPHRASE" -o "C:\OUTPUTFILENAME" -d "C:\FILETODECRYPT.GPG"'
ShellExecute(progname, params, '', @NORMAL, '')
Deana F.
Technical Support
Wilson WindowWare Inc.

MW4

  • Full Member
  • ***
  • Posts: 143
Re: File decryption using GPG command line
« Reply #2 on: January 29, 2014, 11:44:57 am »
That's basically where I was...however your code was very nicely done and buttoned up  :)

However, that gives me the same issue, it opens up a dialog that asks for the passphrase.

I think this post on the internet has the answer, but I can't figure out a way to implement it:

>  Is there an option, eg. --passphrase, that I can use so that I can
>pass the passphrase in the command line when doing a signing, symmetric
>encryption or decryption? Without this option, I will be prompted on the
>console.

No, you'll have to pipe it through a file descriptor with --passphrase-fd.
But with the echo command it can be done on a commandline too on fd 0:
echo password | gpg --passphrase-fd 0 --decrypt / --encrypt.

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: File decryption using GPG command line
« Reply #3 on: January 29, 2014, 12:15:08 pm »
First confirm that you can run this commandline from the command shell cmd.exe:

Obviously modify to fit your needs:
Code: [Select]
echo thisismypassphrase | gpg --batch --passphrase-fd 0 --decrypt "C:\FILETODECRYPT.GPG"
Once you have a working commandline you can convert to a ShellExecute function.

Code: Winbatch
;Sample Commandline
;echo thisismypassphrase | gpg --batch --passphrase-fd 0 --decrypt "C:\FILETODECRYPT.GPG"

;To explicitly run the 32-bit version of a command:
cmd_32 = DirWindows(0):"syswow64\CMD.EXE"
;       To explicitly run the 64-bit version of a command:
cmd_64 = DirWindows(0):"sysnative\CMD.EXE"

DirChange('c:\tools\gpg\') ;Location of gpg.exe
ShellExecute(cmd_32, '/k echo thisismypassphrase | gpg.exe --batch --passphrase-fd 0 --decrypt "C:\FILETODECRYPT.GPG"', '', @NORMAL, '')
Exit
      


Deana F.
Technical Support
Wilson WindowWare Inc.

MW4

  • Full Member
  • ***
  • Posts: 143
Re: File decryption using GPG command line
« Reply #4 on: January 29, 2014, 12:37:57 pm »
That bombs on XP box, what's the /k ?

This works from my command line:

echo MyPassphrase|gpg -o c:\testme_pgp.txt --batch --passphrase-fd 0 --decrypt c:\testpgp.txt.gpg

MW4

  • Full Member
  • ***
  • Posts: 143
Re: File decryption using GPG command line
« Reply #5 on: January 29, 2014, 01:21:25 pm »
I've tried so many combinations I'm spinning...

the only thing that works is this from the command line:

echo MyPassphrase|gpg -o c:\testme_pgp.txt --batch --passphrase-fd 0 --decrypt c:\testpgp.txt.gpg

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: File decryption using GPG command line
« Reply #6 on: January 29, 2014, 02:00:43 pm »
That bombs on XP box, what's the /k ?

This works from my command line:

echo MyPassphrase|gpg -o c:\testme_pgp.txt --batch --passphrase-fd 0 --decrypt c:\testpgp.txt.gpg

/k tells the command shell window to Keep open.

Maybe try:
Code: Winbatch
ShellExecute(cmd_32, '/k echo MyPassphrase|gpg -o c:\testme_pgp.txt --batch --passphrase-fd 0 --decrypt c:\testpgp.txt.gpg', '', @NORMAL, '')
 
Deana F.
Technical Support
Wilson WindowWare Inc.

MW4

  • Full Member
  • ***
  • Posts: 143
Re: File decryption using GPG command line
« Reply #7 on: January 29, 2014, 02:36:13 pm »
This fixed it:

cmd_32 = DirWindows(0):"\system32\\CMD.EXE"

so how do I finish up by closing the CMD box?

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: File decryption using GPG command line
« Reply #8 on: January 29, 2014, 02:50:47 pm »
This fixed it:

cmd_32 = DirWindows(0):"\system32\\CMD.EXE"

so how do I finish up by closing the CMD box?

You will need to remove the double backslash it should read:

Code: Winbatch
cmd_32 = DirWindows(0):"system32\CMD.EXE"
 

or

Code: Winbatch
cmd_32 = DirWindows(0):"syswow64\CMD.EXE"
 

Remove the /k
Deana F.
Technical Support
Wilson WindowWare Inc.

MW4

  • Full Member
  • ***
  • Posts: 143
Re: File decryption using GPG command line
« Reply #9 on: January 29, 2014, 03:17:36 pm »
Double slash was a typo.

When I remove the /K then nothing happens at all.

Winclose('cmd') doesn't work either.



MW4

  • Full Member
  • ***
  • Posts: 143
Re: File decryption using GPG command line
« Reply #10 on: January 29, 2014, 03:22:48 pm »
this got it:

Winclose('C:\WINDOWS\system32\cmd.exe')


AS ALWAYS...THANKS for all the help!!!!

MW4

  • Full Member
  • ***
  • Posts: 143
Re: File decryption using GPG command line
« Reply #11 on: January 29, 2014, 03:27:28 pm »
Final code:
 :)
Code: Winbatch
cmd_32 = DirWindows(0):"\system32\cmd.exe"  
DirChange('C:\Program Files\GNU\GnuPG\') ;Location of gpg.exe
ShellExecute(cmd_32, '/k echo MyPassphrase|gpg -o c:\Decrypted.txt --batch --passphrase-fd 0 --decrypt c:\Encrypted.txt.gpg', '', @NORMAL, '')
Winclose('C:\WINDOWS\system32\cmd.exe')
Exit

stanl

  • Pundit
  • *****
  • Posts: 668
Re: File decryption using GPG command line
« Reply #12 on: February 08, 2014, 04:01:28 am »
If you ever consider compiling the code you might want to use a batch file with replaceable parameters for the encrypted/decrypted files rather than having them hard-coded.

MW4

  • Full Member
  • ***
  • Posts: 143
Re: File decryption using GPG command line
« Reply #13 on: April 02, 2014, 12:01:33 pm »
This is just the meat of the process.
The final product will be in an exe which will do much more than just decrypt the one static file.