Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}

`

like image 673
Kishore Avatar asked Dec 14 '25 11:12

Kishore


1 Answers

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.

like image 154
Eugene Mayevski 'Callback Avatar answered Dec 17 '25 00:12

Eugene Mayevski 'Callback