PGP Decryption using C#

Sorry if the PGP meme is getting old, I had to write some new code yesterday that decrypted a file full of ciphertext and I didn’t see any other examples on the net, so it gets posted here for posterity. Just to frame the issue, the ciphertext is actually stored in the database, so I first extract the ciphertext from a text column, write the ciphertext to a file, decrypt the file, read the results and then delete the file:

// get ciphertext from DB
// and write to a file
// ...
string passphrase = "my_passphrase";
string filename = "myapp\encrypted.asc";
ProcessStartInfo psi = new ProcessStartInfo("pgp");
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.Arguments = filename + " -m -z " + passphrase;
Process process = Process.Start(psi);
string line = null;
string message = null;
while((line = process.StandardOutput.ReadLine()) != null) {
message = line;
}
Console.WriteLine("message = " + message);

If you’re scoring at home, I used the ProcessStartInfo and Process classes from the .NET Framework to invoke pgp.exe from the command line passing the -m flag so that the decrypted message is printed out to the screen (instead of pgp.exe decrypting the message to a new file) and passing the -z flag so that I can send the passphrase as an argument as well. In my project the message is only one line so I iterate over the lines of output until I get to the last line… where the message is then saved in the message string instance.

Peeling away the code, you end up with this:

C:\pgp6.5.8>pgp c:\myapp\encrypted.asc -m -z my_passphrase
Pretty Good Privacy(tm) Version 6.5.8
(c) 1999 Network Associates Inc.
Uses the RSAREF(tm) Toolkit, which is copyright RSA Data Security, Inc.
Export of this software may be restricted by the U.S. government.
moreflagFile is encrypted. Secret key is required to read it.
Key for user ID: Aaron Johnson
1024-bit DSS key, Key ID ******, created 2004/09/02
Key can sign.
Just a moment...
this is the message

A caveat: if you run the above code from an ASP.NET web application make sure that ASPNET user has access to the private key.

By the way, the folks over at Bouncy Castle have a C# port of the their excellent Java encryption libraries, but it doesn’t appear that the org.bouncycastle.openpgp package has been ported just yet, otherwise I would have used that.

11 thoughts on “PGP Decryption using C#”

  1. FYI… you might want to check the process.ExitCode before you do anything with the message (non-zero exit code means the operation was unsuccessful). Also, you should dispose the process when you’re done (or better, use a using)).

  2. Thanks for this fine example.

    I’m new to C# programming. Does anyone know how to suppress or close the DOS window? I have tried psi.CreateNoWindow, but that does not seem to work.

  3. error msg:
    moreflagFile is encrypted. Secret key is required to read it. You do not have the secret key needed to decrypt this file.

    how can i solve this? please help me… plss….

  4. error msg:
    moreflagFile is encrypted. Secret key is required to read it. You do not have the secret key needed to decrypt this file.

    how can i solve this? please help me… plss….

  5. thanks for your blog on how to use command line with C#. as a console app, this program run without a hitch but i cant get it to run when i use it in my asp.net application. how do i ensure that the ASPNET user has access to the private key?

    thanks

  6. I’m also new to this. I don’t quite understand how this works. I need to write a program to go out to a file, grab a .pgp, decrypt it, then store it in a different file.

Leave a Reply

Your email address will not be published. Required fields are marked *