Click here to Skip to main content
12,406,687 members (60,422 online)
Click here to Skip to main content
Add your own
alternative version

Stats

413K views
7.3K downloads
91 bookmarked
Posted

Gnu Privacy Guard (GPG/PGP) for .NET [v1.0]

, 18 Sep 2003
Rate this:
Please Sign up or sign in to vote.
This article provides a C# wrapper class (GnuPGWrapper) that will enable use of GnuPG (the OpenPGP Internet encryption standard) within a .NET application.

Sample Image - GnuPGDotNet.jpg

Introduction

This article presents GnuPGWrapper v1.0, a wrapper class for GnuPG.

GnuPG stands for GNU Privacy Guard and is GNU's tool for secure communication and data storage. It can be used to encrypt data and to create digital signatures. It includes an advanced key management facility and is compliant with the proposed OpenPGP Internet standard as described in RFC 2440. As such, GnuPG is a complete and free replacement for PGP (Pretty Good Privacy).

This article provides a C# wrapper class (GnuPGWrapper) that will enable use of OpenPGP Internet encryption standard within a .NET world. It is shipped with a demo ASP.NET Web Form (GnuPG.aspx) which calls the wrapper class.

Installation

Prerequisites

  • Complete .NET Environment, e.g. Windows XP Professional + IIS 5.0 + .NET Framework SDK
  • GnuPG for Windows (more about GnuPG)

Procedure

Background

GnuPG ships as a command line program (gpg.exe) acting as a filter (reads from standard input and writes into standard output). Although suitable for scripting on UNIX systems (where calling a command line program from sh or bash is easy), it's pretty hard to integrate this in a production .NET environment.

The GnuPG Wrapper executes the command line program (gpg.exe) in a different process, redirects standard input (stdin), standard output (stdout) and standard error (stderr) streams, and monitors the streams to fetch the results of the encryption/signing operation.

Please note that you must have INSTALLED GnuPG AND generated/imported the appropriate keys before using this class. Refer to the GnuPG manual to do this...

Using the code

In order to use the wrapper class, you need to proceed as follows:

  1. Create an instance of the class
  2. Set the command property to the requested command (SignAndEncrypt, Encrypt, Decrypt, Sign, Verify)
  3. Optionally, set parameters for the command (home directory, originator, recipients, etc...)
  4. Call the ExecuteCommand method with input/output strings variables

The next sections show sample source code for the most command operation (SignAndEncrypt, Decrypt, Verify).

Encrypt and Sign

// Reference My GnuPG wrapping class
using Emmanuel.Cryptography.GnuPG;

// Create GnuPG wrapping class
GnuPGWrapper gpg = new GnuPGWrapper();

// Set command
gpg.command = Commands.SignAndEncrypt;

// Set some parameters from on Web.Config file
gpg.homedirectory = Server.MapPath
       (ConfigurationSettings.AppSettings["homedirectory"]);
gpg.passphrase = ConfigurationSettings.AppSettings["passphrase"];

// Set other parameters from Web Controls
gpg.originator = FromTextBox.Text;
gpg.recipient = ToTextBox.Text;

// Declare input/output variables
// (input is also read from a Web control)
string inputText = MessageTextBox.Text;
string outputText = "";

// Execute GnuPG
gpg.ExecuteCommand(inputText, out outputText);

// Display output text
OutputTextBox.Text = outputText;
OutputTextBox.Visible = true;
ErrorMessage.Visible = false;
ExitCodeLabel.Text = gpg.exitcode.ToString();

Decrypt

using Emmanuel.Cryptography.GnuPG;

GnuPGWrapper gpg = new GnuPGWrapper();

gpg.homedirectory = "C:\Inetpub\wwwroot\GnuPGDotNet\GnuPG"
gpg.passphrase = "My passphrase is so cool I can't remember it"
gpg.command = Commands.Decrypt;

// Execute GnuPG
string outputText = "";
gpg.ExecuteCommand("This is a test message.", out outputText);

// Display output text
[...]

Verify

using Emmanuel.Cryptography.GnuPG;
GnuPGWrapper gpg = new GnuPGWrapper();

gpg.homedirectory = "C:\Inetpub\wwwroot\GnuPGDotNet\GnuPG"
gpg.passphrase = "My passphrase is so cool I can't remember it"
gpg.originator = "me@mycompany.com";
gpg.command = Commands.Verify;

// Execute GnuPG
string outputText = "";
gpg.ExecuteCommand("This is a test message.", out outputText);

// Display output text
[...]

Error handling

Error handling is done via a specific Exception class; method ExecuteCommand raises this exception whenever an error occurs. You calling application can handle this exception as follows:

using Emmanuel.Cryptography.GnuPG;

    try 
    {

        GnuPGWrapper gpg = new GnuPGWrapper();

        gpg.homedirectory = "C:\Inetpub\wwwroot\GnuPGDotNet\GnuPG"
        gpg.passphrase = "My passphrase is so cool I can't remember it"
        gpg.originator = "me@mycompany.com";
        gpg.recipient = "you@yourcompany.com";
        gpg.command = Commands.SignAndEncrypt;

        // Execute GnuPG
        string outputText = "";
        gpg.ExecuteCommand("This is a test message.", out outputText);

        // Display output text
        [...]

    }
    catch (GnuPGException gpge)
    {
        // Display error message
        // Contains a clear text error message, 
        // either from the wrapper or from gpg.exe itself
        ErrorMessage.Text = gpge.Message; 
    }

Points of interest

The GnuPG wrapper:

  • Doesn't use any temporary files to store results; it directly uses streams/pipes.
  • Uses multiple threads to read data from standard input and standard error, preventing any deadlocks.
  • Uses configurable timeouts to prevent blocking calling applications in case of a system/program/process crash
  • Uses a configurable passphrase, which can be stored in a local configuration file (Web.Config) to prevent disclosure of the phrase

About GnuPG and PGP

This class has been developed and tested with GnuPG v1.2.0 (MingW32).

You can check the command line manual page for gpg.exe.

For more about GNU, please refer to http://www.gnu.org/. For more about GnuPG, please refer to http://www.gnupg.org/. For more about OpenPGP (RFC 2440), please refer to http://www.gnupg.org/rfc2440.html. For more about PGP, please refer to http://www.pgpi.org/.

History

  • Date posted: October 30th, 2002
  • Updated: September 19th, 2003

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Emmanuel Kartmann
Web Developer
France France
Fell into computer software at the age of 11, founder of 3 startups, and now manager of an independent software vendor (ISV) labelled proSDK (www.prosdk.com)... And still a freeware writer and technical article author!

You may also be interested in...

Comments and Discussions

 
GeneralRe: Decrypt Error Pin
kwhe9718-Oct-07 10:32
memberkwhe9718-Oct-07 10:32 
QuestionKeys vs. Passphrase?? Pin
Jeff960012-Jul-07 8:37
memberJeff960012-Jul-07 8:37 
QuestionIs this possible? Pin
rkd803-Jul-07 5:56
memberrkd803-Jul-07 5:56 
AnswerRe: Is this possible? Pin
i6189-Jul-07 13:53
memberi6189-Jul-07 13:53 
GeneralProblems with pgp.exe Pin
bevans197526-Jun-07 5:00
memberbevans197526-Jun-07 5:00 
GeneralRe: Problems with pgp.exe Pin
i6189-Jul-07 13:49
memberi6189-Jul-07 13:49 
QuestionKeyblock resource file open error Pin
Christopher Payne7-Jun-07 12:04
memberChristopher Payne7-Jun-07 12:04 
AnswerRe: Keyblock resource file open error Pin
Hoots9-Jul-07 5:13
memberHoots9-Jul-07 5:13 
GeneralRe: Keyblock resource file open error Pin
Christopher Payne9-Jul-07 10:31
memberChristopher Payne9-Jul-07 10:31 
GeneralRe: Keyblock resource file open error Pin
Hoots10-Jul-07 10:51
memberHoots10-Jul-07 10:51 
GeneralRe: Keyblock resource file open error Pin
Christopher Payne2-Aug-07 8:49
memberChristopher Payne2-Aug-07 8:49 
GeneralRe: Keyblock resource file open error Pin
Hoots20-Aug-07 0:31
memberHoots20-Aug-07 0:31 
Generalproblem with gpg.exe Pin
ashish84125-Apr-07 21:11
memberashish84125-Apr-07 21:11 
GeneralRe: problem with gpg.exe Pin
i6189-Jul-07 13:47
memberi6189-Jul-07 13:47 
GeneralProblem in decryption Pin
Prathapavidyadaran9-Apr-07 2:38
memberPrathapavidyadaran9-Apr-07 2:38 
QuestionProblem in decrypting the binary files Pin
kalai834-Apr-07 6:10
memberkalai834-Apr-07 6:10 
AnswerRe: Problem in decrypting the binary files Pin
jhance5-Apr-07 13:31
memberjhance5-Apr-07 13:31 
GeneralRe: Problem in decrypting the binary files Pin
kalai836-Apr-07 0:53
memberkalai836-Apr-07 0:53 
GeneralRe: Problem in decrypting the binary files Pin
jhance6-Apr-07 1:09
memberjhance6-Apr-07 1:09 
GeneralRe: Problem in decrypting the binary files Pin
PhoenixHawke17-Sep-07 7:31
memberPhoenixHawke17-Sep-07 7:31 
AnswerRe: Problem in decrypting the binary files Pin
keval.devani30-Sep-08 23:45
memberkeval.devani30-Sep-08 23:45 
QuestionError when trying to implement this in a windows app Pin
toinette11-Oct-06 8:10
membertoinette11-Oct-06 8:10 
GeneralStandardInput.Write Pin
wilpeck16-Aug-06 16:59
memberwilpeck16-Aug-06 16:59 
AnswerRe: StandardInput.Write Pin
i6189-Jul-07 13:44
memberi6189-Jul-07 13:44 
GeneralRe: StandardInput.Write Pin
DJ Parker27-Apr-10 3:57
memberDJ Parker27-Apr-10 3:57 
GeneralProblem-Im totally new with this Pin
Anondo11-Jun-06 1:09
memberAnondo11-Jun-06 1:09 
GeneralRe: Problem-Im totally new with this Pin
Bharat Gadhia23-Aug-06 10:54
memberBharat Gadhia23-Aug-06 10:54 
GeneralProblem calling from VB.net 1.1 Pin
veraperezp7-May-06 14:23
memberveraperezp7-May-06 14:23 
GeneralRe: Problem calling from VB.net 1.1 Pin
veraperezp7-May-06 14:36
memberveraperezp7-May-06 14:36 
AnswerRe: Problem calling from VB.net 1.1 [modified] Pin
esambo2-Nov-06 9:28
memberesambo2-Nov-06 9:28 
GeneralProblem - recipient names containing spaces Pin
Adam C25-Jan-06 2:43
memberAdam C25-Jan-06 2:43 
GeneralHelp needed Pin
nadarajan1-Dec-05 9:32
membernadarajan1-Dec-05 9:32 
AnswerRe: Help needed Pin
esambo2-Nov-06 10:02
memberesambo2-Nov-06 10:02 
QuestionHow do I install a key for the webserver account Pin
Jeffster15-Nov-05 14:13
memberJeffster15-Nov-05 14:13 
AnswerRe: How do I install a key for the webserver account Pin
Adam C25-Jan-06 2:35
memberAdam C25-Jan-06 2:35 
GeneralDecrypt File Pin
billblair9-Nov-05 4:11
memberbillblair9-Nov-05 4:11 
GeneralJorge Ruiz (Colombia) Pin
georgeruiz10-Oct-05 11:33
membergeorgeruiz10-Oct-05 11:33 
GeneralDecrypt file error Pin
Willanchen11-Jul-05 22:23
memberWillanchen11-Jul-05 22:23 
GeneralRe: Decrypt file error Pin
bigrichid29-Jul-05 8:10
memberbigrichid29-Jul-05 8:10 
GeneralRe: Decrypt file error Pin
jtmcgowan15-May-06 8:46
memberjtmcgowan15-May-06 8:46 
GeneralGenerating new keys/editing existing ones Pin
Gosharu2-Jul-05 9:08
memberGosharu2-Jul-05 9:08 
GeneralSending password is not responding Pin
naimul29-Jun-05 7:19
membernaimul29-Jun-05 7:19 
GeneralRe: Sending password is not responding Pin
Gosharu2-Jul-05 8:57
memberGosharu2-Jul-05 8:57 
GeneralFix for size limitation on Standard Input Pin
aziruth20-Apr-05 11:14
memberaziruth20-Apr-05 11:14 
GeneralRe: Fix for size limitation on Standard Input Pin
robroe4-Oct-06 3:47
memberrobroe4-Oct-06 3:47 
GeneralRe: Fix for size limitation on Standard Input Pin
varunkorpol24-Jun-09 12:04
membervarunkorpol24-Jun-09 12:04 
GeneralRe: Fix for size limitation on Standard Input Pin
robroe24-Jun-09 23:42
memberrobroe24-Jun-09 23:42 
QuestionBinary? Pin
rcelliott18-Apr-05 19:09
memberrcelliott18-Apr-05 19:09 
QuestionHow to encrypt by existing public key? Pin
bask190728-Feb-05 6:08
memberbask190728-Feb-05 6:08 
AnswerRe: How to encrypt by existing public key? Pin
Anonymous29-May-05 5:28
sussAnonymous29-May-05 5:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 19 Sep 2003
Article Copyright 2002 by Emmanuel Kartmann
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid