Writing to a Process

B

Bill Gregg

I'm trying to decrypt a file using GPG and starting it via a
system.diagnostics.process. The required steps are:

1) Launch the GPG.exe with the correct parameters indicating what file
to decrypt.
2) Once GPG.exe begins, it prompts for the passphrase associated with
the key.

So in my C# code, I've created a process, configured the StartInfo for
the process to allow StandardInRedirect, started the process, and tried
to write the passphrase to the process using the StandardIn stream. But
nothing is ever sent to the process window. I've sent the passphrase
and flushed the stream 10 times in a row, but nothing ever gets written
to the process window. How can I pass a string to a process window?

Help,
Bill

See sample code:

using DotNetProcess = System.Diagnostics.Process;

string app = "gpg.exe";
string parms = " --output "+this.zipFile+" --yes --decrypt
"+this.targetFile;

DotNetProcess myProcess = new DotNetProcess();
myProcess.StartInfo.FileName =app;
myProcess.StartInfo.RedirectStandardInput=true;
myProcess.StartInfo.Arguments=parms;
myProcess.StartInfo.UseShellExecute=false;
myProcess.Start();

myProcess.StandardInput.WriteLine(EncryptionParms.PassPhrase);
myProcess.StandardInput.Flush();
 
J

Jani Järvinen [MVP]

Bill,
I'm trying to decrypt a file using GPG and starting it via a
system.diagnostics.process.

I haven't tried to automate GPG, but few things come into mind as possible
obstacles on your way:

1. Are you sure GPG does accept input from the standard input for the
passphrase? It might well be that the program requires direct input from the
keyboard as this would in fact provide additional security to GPG. Try
feeding GPG a simple text file from the command line.

2. Try testing your C# code with another console application. An example is
given in MSDN:

http://msdn.microsoft.com/library/d...sstartinfoclassredirectstandardinputtopic.asp

3. Make sure you are sending the Enter character after you've sent in the
passphrase. You are using the WriteLine method which should work, but GPG
can get confused because both the CR and LF characters. Try sending only
either. I don't think this is the issue, but it might be when all else
fails.

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
B

Bill Gregg

Thanks for the feedback Jani. I'm beginning to think that you are right
about GPG not reading from StandardIn, but only accepting input from the
keyboard.

I went to www.gnupg.org and checked ther FAQ. They said that I should
just not use a passphrase, because including it in a file or in an
application would not be safe anyway.

So my workaround was, I removed the passphrase from my key.

gpg.exe --edit-key
passwd

Thanks,
Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top