Take the 2-minute tour ×

I'm using the command line to encrypt files that I am sending out but I am trying to figure out how to use the same method to decrypt them. If I run the command it get prompted for the passphrase, but I don't see a way to pass in the passphrase using the command line. Here is how I am encrypting the file:

var proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.WorkingDirectory = "C:\\";
proc.StartInfo.FileName = @"C:\Progra~1\GNU\GnuPG\gpg.exe";
proc.StartInfo.Arguments = @"-e -u ""user1@example.com"" -r ""user2@example.com"" ""C:\file.csc""";

** Here is a useful link that was used for my solution: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/38c21304-fc7a-42cc-a5fb-dcb6da7f6411/

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

The property Process.StandardInput should give you a StreamWriter that you can use to provide the passphrase on standard input.

share|improve this answer
I tried to get this to work by adding the following lines using the MSDN example but it doesn't seem to be sending the passphrase to the display. I have to manually type them in to the cmd window. proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.Start(); StreamWriter myStreamWriter = proc.StandardInput; myStreamWriter.Write("1234"); myStreamWriter.Close(); –  Scott Nov 1 '10 at 18:24
I got it to work now. proc.Start(); proc.StandardInput.WriteLine(password); proc.StandardInput.Flush(); –  Scott Nov 1 '10 at 18:46
add comment

GPG on Linux has --passphrase-fd N, where N is int. If N == 0, the passphrase is read from standard input.

Not sure if the same applies on Windows but might be worth checking out.

share|improve this answer
add comment

Use --passphrase-fd 0 to get GPG to take the passphrase from stdin and then pass it in using a pipe.

echo 123456| gpg --passphrase-fd 0 -e -u user1@example.com -r user2@example.com C:\file.csc


gpg --passphrase-fd 0 -e -u user1@example.com -r user2@example.com --passphrase-fd 2 file.csc < password.file

Make sure you don't pass any extra spaces on stdin somehow as GPG doesn't handle those well.

I'm not sure how you can pass something on stdin on .NET, so you'll have to fill the gap there.

share|improve this answer
I got the first one to work but my passphrase contains a '>' so I used the example with another file that has a passphrase that doesn't contain this character. I got the original one to work using --passphrase-file "pass.txt" but its hardly secure with my passphrase in a text file... –  Scott Nov 1 '10 at 18:36
add comment

Your Answer


