Command line GPG decrypting using c# - passphrase?

2019-05-26 23:05发布

问题:

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""";
proc.Start();
proc.WaitForExit();

** 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/

回答1:

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



回答2:

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.



回答3:

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

or

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.



标签: c# .net gnupg