File decryption using GPG command line

Started by MW4, January 29, 2014, 11:17:43 AM

Previous topic - Next topic

MW4

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

I am not personally familiar with GPG. However maybe you can give this a try:

Code (winbatch) Select
;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

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

First confirm that you can run this commandline from the command shell cmd.exe:

Obviously modify to fit your needs:
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) Select
;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

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

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

Quote from: MW4 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

/k tells the command shell window to Keep open.

Maybe try:
Code (winbatch) Select
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

This fixed it:

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

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

Deana

Quote from: MW4 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?

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

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


or

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


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

MW4

Double slash was a typo.

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

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



MW4

this got it:

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


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

MW4

Final code:
:)
Code (winbatch) Select
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

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

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.