Kishore
Kishore

Reputation: 31

PGP Encrypt from C# using GnuPG

I'm trying to encrypt a incoming document in C#, and I'm using GnuPG with input redirection. I need to use -se(sign and encrypt) in a single step, which requires entering passphrase. But for some reason, input redirection is not working. Appreciate your help. Control is going to else block. I'm not sure if there is deadlock or child process(gpg.exe) waiting for the input.

pgpproc = new Process();
pgpproc.StartInfo.FileName = exeFilePath;
pgpproc.StartInfo.RedirectStandardOutput = false;
pgpproc.StartInfo.RedirectStandardInput = true;
pgpproc.StartInfo.UseShellExecute = false;
pgpproc.StartInfo.Arguments = "-a  -o C:\PGPStaging\output.pgp -se -r recipientID C:\PGPStaging\input.txt";
pgpproc.StartInfo.CreateNoWindow = true;
pgpproc.Start();
StreamWriter myStreamWriter = pgpproc.StandardInput;
myStreamWriter.Write("*****");
myStreamWriter.Close();

if (pgpproc.WaitForExit(waittime))
{
  //success
}
else
{
  //failure
  pgpproc.Kill();
  pgpproc.WaitForExit();
}

`

Upvotes: 3

Views: 2228

Answers (3)

Matt Mattingly
Matt Mattingly

Reputation: 1

I had the same problem when I was having to perform some decryption. What I wound up doing is Piping the passphrase like so.

    procStartInfo.Arguments = @"/c echo " + passphrase + @"| c:\gnupg\gpg --passphrase-fd --     decrypt --output " + "\"" + fileLocationToDecryptTo + "\"" + " \"" + fileLocationToDecryptFrom     + "\"";

It seems to work fairly well.

Upvotes: 0

rkhayrov
rkhayrov

Reputation: 10260

Try adding --passphrase-fd 0 to the GnuPG command line to instruct it to read password from the standard input.

Upvotes: 1

The first thing you can do is remove redirection and see the console yourself in order to get to know, what gnupg is doing / waiting. Next, try adding new-line character (\n or 0x13 or 0x130x10, try all variants) to the end of the password - when you type something, you press Enter to pass it, and you must do the same here as well. Finally, you can use OpenPGPBlackbox to perform the operation without external applications.

Upvotes: 1

Related Questions