5,667,575 members and growing! (9,971 online)
Email Password   helpLost your password?
General Programming » Cryptography & Security » Security     Advanced

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

By Emmanuel Kartmann

This article provides a C# wrapper class (GnuPGWrapper) that will enable use of GnuPG (the OpenPGP Internet encryption standard) within a .NET application.
C#.NET 1.0, Win2K, WinXP, Windows, .NET, Visual Studio, Dev

Posted: 29 Oct 2002
Updated: 18 Sep 2003
Views: 188,077
Bookmarked: 62 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 5.48 Rating: 4.46 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 7.1%
3
1 vote, 7.1%
4
12 votes, 85.7%
5

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

About the Author

Emmanuel Kartmann


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!

Occupation: Web Developer
Location: France France

Other popular Cryptography & Security articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 112 (Total in Forum: 112) (Refresh)FirstPrevNext
GeneralError when calling the ExecuteCommandmembermpstrick@gmail.com6:29 30 Sep '08  
Ranthomedirectory sets _bindirectory, which guarantees "System cannot find the file specified" errormembermcgmatt18:35 17 Sep '08  
Generalgpg4win - doesn't encrypt a file over 900kbmemberwbcintsol16:22 19 May '08  
GeneralRe: gpg4win - doesn't encrypt a file over 900kbmemberMember 44863223:45 27 May '08  
Generalpublic key not found: problem and solution [modified]memberStephen Watson1:44 30 Jul '07  
GeneralRe: public key not found: problem and solutionmembertopherino4:28 7 Sep '07  
QuestionRe: public key not found: problem and solutionmemberroopam_vishwam20:43 6 May '08  
GeneralDecrypt Error [modified]memberDrivenTooFar10:12 23 Jul '07  
GeneralRe: Decrypt Errormemberkwhe9711:32 18 Oct '07  
QuestionKeys vs. Passphrase??memberJeff96009:37 12 Jul '07  
GeneralIs this possible?memberrkd806:56 3 Jul '07  
GeneralRe: Is this possible?memberi61814:53 9 Jul '07  
GeneralProblems with pgp.exememberbevans19756:00 26 Jun '07  
GeneralRe: Problems with pgp.exememberi61814:49 9 Jul '07  
QuestionKeyblock resource file open errormemberChristopher Payne13:04 7 Jun '07  
AnswerRe: Keyblock resource file open errormemberHoots6:13 9 Jul '07  
GeneralRe: Keyblock resource file open errormemberChristopher Payne11:31 9 Jul '07  
GeneralRe: Keyblock resource file open errormemberHoots11:51 10 Jul '07  
GeneralRe: Keyblock resource file open errormemberChristopher Payne9:49 2 Aug '07  
GeneralRe: Keyblock resource file open errormemberHoots1:31 20 Aug '07  
Generalproblem with gpg.exememberashish84122:11 25 Apr '07  
GeneralRe: problem with gpg.exememberi61814:47 9 Jul '07  
GeneralProblem in decryptionmemberPrathapavidyadaran3:38 9 Apr '07  
QuestionProblem in decrypting the binary filesmemberkalai837:10 4 Apr '07  
AnswerRe: Problem in decrypting the binary filesmemberjhance14:31 5 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Sep 2003
Editor: Nishant Sivakumar
Copyright 2002 by Emmanuel Kartmann
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project