Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GPG decryption via script?

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
I have a PGP-encrypted file which I download and decrypt on my machine (currently via the GPG Windows GUI). Call the file 'MyFile.gpg'.

I want to automate the decryption of this file. However I can't seem to locate a reference detailing the command-line syntax necessary, though I did find the manual here:
I'm not sure what the parameters mean or how to structure the statement I'll need. First, is there a reference online simply outlining the command-line syntax for GPG under Windows (the manual I linked seems to be for Linux)?

Second, what is the exact syntax I'd use to decrypt 'C:\MyFile.gpg' to 'C:\MyDecryptedFile.xxx'? How do I point toward my private key? I will ultimately wrap this in a VBScript and will pass my passphrase via an InputBox.

Thanks!
 
Well, that's really a question about GPG, not about VBS, isn't it? :)
First things first.. As I don't use GPG on Windows, first we need to make sure the command-line syntax is similar.

Can you do this?
c:\>...\>gpg --help

And get a help output, or do you get an error? Do of course replace 'gpg' with the name of the actual command-line executable..

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
Yeah I suppose it is more appropriately a GPG question, though I'm not sure if there's a forum for that?

However my thought was that if anyone had implemented it via command-line they'd do so via either VBScript or a DOS batch file, and since I'm doing the former this seemed the place to post.

Yes, the 'help' syntax you posted works; thanks.

From the GPG directory I ran "gpg -d "C:\MyFile.pgp". It prompted me for my passphrase, then decrypted the file in the command-window.

Not sure from the help items how to:
a) Send the passphrase as a parameter
b) Send the decrypted output to a file
 
I got it to output to a file like so:

"C:\Program Files\GNU\GnuPG\gpg.exe" -d "C:\MiFile.pgp" > "C:\MyFile.txt"

Now the only piece of the puzzle is passing the passphrase as a parameter via command-line.
 
Yeah, and that's really kinda tricky, since that's really the bit that makes GPG secure.. ;-) You might be able to make GPG somehow wait for input and use some Windows trick to pipe whatever you type into the command line?
On this particular bit, I am out of my league.

Tao Te Ching Discussions : Chapter 9 (includes links to previous chapters)
What is the nature of conflict?
 
Got it!

This is the command-line syntax:
"C:\Program Files\GnuPG\gpg.exe" --passphrase "My passphrase" -o "C:\MyFile.txt" -d "C:\MyFile.pgp"

Here is a VBScript example:
Code:
Option Explicit

Dim bWaitOnReturn: bWaitOnReturn = True
Dim iWindowStyle: iWindowStyle = 7 'Minimized; active window stays active
Dim sPassphrase: sPassphrase = InputBox("Enter passphrase","Passphrase")
Dim sFileName_Output: sFileName_Output = "C:\MyDecryptedFile.txt"
Dim sFileName_Input: sFileName_Input = "C:\MyEncryptedFile.pgp"
Dim sCommand_Text: sCommand_Text = Chr(34) & "C:\Program Files (x86)\GNU\GnuPG\gpg.exe" & Chr(34) & "--passphrase " & Chr(34) & sPassphrase & Chr(34) & " -o " & Chr(34) & sFileName_Output & Chr(34) & " -d " & Chr(34) & sFileName_Input & Chr(34)
Dim oWiSH_Shell: Set oWiSH_Shell = CreateObject("WScript.Shell")
oWiSH_Shell.Run sCommand_Text, iWindowStyle, bWaitOnReturn
Set oWiSH_Shell = Nothing
WScript.Quit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top