Click here to Skip to main content
15,897,518 members
Articles / Security / Encryption

PGP Encryption with C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (23 votes)
6 Jan 2013CPOL7 min read 244.2K   6.7K   62  
Encrypting and Decrypting Files with GnuPG
<?xml version="1.0"?>
<doc>
    <assembly>
        <name>Starksoft.Cryptography.OpenPGP</name>
    </assembly>
    <members>
        <member name="T:Starksoft.Cryptography.OpenPGP.GnuPGKey">
            <summary>
            Class structure that proves a read-only view of the GnuPG keys. 
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGKey.#ctor(System.String)">
            <summary>
            GnuPGKey constructor.
            </summary>
            <param name="raw">Raw output stream text data containing key information.</param>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKey.Key">
            <summary>
            Key text information.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKey.KeyExpiration">
            <summary>
            Key expiration date and time.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKey.UserId">
            <summary>
            Key user identification.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKey.UserName">
            <summary>
            Key user name.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKey.SubKey">
            <summary>
            Sub-key information.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKey.SubKeyExpiration">
            <summary>
            Sub-key expiration data and time.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKey.Raw">
            <summary>
            Raw output key text generated by GPG.EXE.
            </summary>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.DecryptAsyncCompletedEventArgs">
            <summary>
            Event arguments class for the DecryptAsyncCompleted event.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.DecryptAsyncCompletedEventArgs.#ctor(System.Exception,System.Boolean)">
            <summary>
            Constructor.
            </summary>
            <param name="error">Exception information generated by the event.</param>
            <param name="cancelled">Cancelled event flag.  This flag is set to true if the event was cancelled.</param>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.EncryptAsyncCompletedEventArgs">
            <summary>
            Event arguments class for the EncryptAsyncCompleted event.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.EncryptAsyncCompletedEventArgs.#ctor(System.Exception,System.Boolean)">
            <summary>
            Constructor.
            </summary>
            <param name="error">Exception information generated by the event.</param>
            <param name="cancelled">Cancelled event flag.  This flag is set to true if the event was cancelled.</param>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection">
            <summary>
            Collection of PGP keys stored in the GnuPGP application.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection.#ctor(System.IO.StreamReader)">
            <summary>
            Constructor.
            </summary>
            <param name="keys">StreamReader object containing GnuPG raw key stream data.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection.IndexOf(Starksoft.Cryptography.OpenPGP.GnuPGKey)">
            <summary>
             Searches for the specified GnuPGKey object and returns the zero-based index of the
             first occurrence within the entire GnuPGKeyCollection colleciton.
            </summary>
            <param name="item">The GnuPGKeyobject to locate in the GnuPGKeyCollection.</param>
            <returns>The zero-based index of the first occurrence of item within the entire GnuPGKeyCollection, if found; otherwise, –1.</returns>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection.GetKey(System.Int32)">
            <summary>
             Retrieves the specified GnuPGKey object by zero-based index from the GnuPGKeyCollection.        
            </summary>
            <param name="index">Zero-based index integer value.</param>
            <returns>The GnuPGKey object corresponding to the index position.</returns>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection.AddKey(Starksoft.Cryptography.OpenPGP.GnuPGKey)">
            <summary>
            Adds a GnuPGKey object to the end of the GnuPGKeyCollection.
            </summary>
            <param name="item">GnuPGKey item to add to the GnuPGKeyCollection.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection.ToDataTable">
            <summary>
            Convert current GnuPGKeyCollection to a DataTable object to make data binding a minpulation of key data easier.
            </summary>
            <returns>Data table object.</returns>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection.Raw">
            <summary>
            Raw key stream text data.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection.Count">
            <summary>
            Gets the number of elements actually contained in the GnuPGKeyCollection.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPGKeyCollection.Item(System.Int32)">
            <summary>
            Indexer for the GnuPGKeyCollection collection.
            </summary>
            <param name="index">Zero-based index value.</param>
            <returns></returns>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.GnuPGException">
            <summary>
            This exception is thrown when a general, unexpected error condition occurs when running the GPG.EXE program.   
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGException.#ctor">
            <summary>
            Constructor.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGException.#ctor(System.String)">
            <summary>
            Constructor.
            </summary>
            <param name="message">Exception message text.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGException.#ctor(System.String,System.Exception)">
            <summary>
            Constructor.
            </summary>
            <param name="message">Exception message text.</param>
            <param name="innerException">The inner exception object.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGException.#ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext)">
            <summary>
            Constructor.
            </summary>
            <param name="info">Serialization information.</param>
            <param name="context">Stream context information.</param>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.OutputTypes">
            <summary>
            GnuPG output itemType.
            </summary>
        </member>
        <member name="F:Starksoft.Cryptography.OpenPGP.OutputTypes.AsciiArmor">
            <summary>
            Ascii armor output.
            </summary>
        </member>
        <member name="F:Starksoft.Cryptography.OpenPGP.OutputTypes.Binary">
            <summary>
            Binary output.
            </summary>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.GnuPG">
            <summary>
            Interface class for GnuPG.
            </summary>
            <remarks>
            <para>
            GNU Privacy Guard from the GNU Project (also called GnuPG or GPG for short) is a highly regarded and supported opensource project that provides a complete and free implementation of the OpenPGP standard as defined by RFC2440. 
            GnuPG allows you to encrypt and sign your data and communication, manage your public and privde OpenPGP keys as well 
            as access modules for all kind of public key directories. GPG.EXE, is a command line tool that is installed with GnuPG and contains features for easy integration with other applications. 
            </para>
            <para>
            The Starksoft OpenPGP Component for .NET provides classes that interface with the GPG.EXE command line tool.  The Starksoft OpenPGP libraries allows any .NET application to use GPG.EXE to encrypt or decypt data using
            .NET IO Streams.  No temporary files are required and everything is handled through streams.  Any .NET Stream object can be used as long as the source stream can be read and the 
            destination stream can be written to.  But, in order for the Starksoft OpenPGP Component for .NET to work you must first install the lastest version of GnuPG which includes GPG.EXE.  
            You can obtain the latest version at http://www.gnupg.org/.  See the GPG.EXE tool documentation for information
            on how to add keys to the GPG key ring and creating your public and private keys.
            </para>
            <para>
            If you are new to GnuPG please install the application and then read how to generate new key pair or importing existing OpenPGP keys. 
            You can rad more about key generation and importing at http://www.gnupg.org/gph/en/manual.html#AEN26
            </para>
            <para>
            Encrypt File Example:
            <code>
            // create a new GnuPG object
            GnuPG gpg = new GnuPG();
            // specify a recipient that is already on the key-ring 
            gpg.Recipient = "myfriend@domain.com";
            // create an IO.Stream object to the source of the data and open it
            FileStream sourceFile = new FileStream(@"c:\temp\source.txt", FileMode.Open);
            // create an IO.Stream object to a where I want the encrypt data to go
            FileStream outputFile = new FileStream(@"c:\temp\output.txt", FileMode.Create);
            // encrypt the data using IO Streams - any type of input and output IO Stream can be used
            // as long as the source (input) stream can be read and the destination (output) stream 
            // can be written to
            gpg.Encrypt(sourceFile, outputFile);
            // close the files
            sourceFile.Close();
            outputFile.Close();
            </code>
            </para>
            <para>
            Decrypt File Example:
            <code>
            // create a new GnuPG object
            GnuPG gpg = new GnuPG();
            // create an IO.Stream object to the encrypted source of the data and open it 
            FileStream encryptedFile = new FileStream(@"c:\temp\output.txt", FileMode.Open);
            // create an IO.Stream object to a where you want the decrypted data to go
            FileStream unencryptedFile = new FileStream(@"c:\temp\unencrypted.txt", FileMode.Create);
            // specify our secret passphrase (if we have one)
            gpg.Passphrase = "secret passphrase";            
            // decrypt the data using IO Streams - any type of input and output IO Stream can be used
            // as long as the source (input) stream can be read and the destination (output) stream 
            // can be written to
            gpg.Decrypt(encryptedFile, unencryptedFile);
            // close the files
            encryptedFile.Close();
            unencryptedFile.Close();
            </code>
            </para>
            </remarks>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.#ctor">
            <summary>
            GnuPG interface class default constructor.
            </summary>
            <remarks>
            The GPG executable location is obtained by information in the windows registry.  Home path is set to the same as the
            GPG executable path.  Output itemType defaults to Ascii Armour.
            </remarks>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.#ctor(System.String,System.String)">
            <summary>
            GnuPG interface class constuctor.
            </summary>
            <remarks>Output itemType defaults to Ascii Armour.</remarks>
            <param name="homePath">The home directory where files to encrypt and decrypt are located.</param>
            <param name="binaryPath">The GnuPG executable binary directory.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.#ctor(System.String)">
            <summary>
            GnuPG interface class constuctor.
            </summary>
            <param name="homePath">The home directory where files to encrypt and decrypt are located.</param>
            <remarks>
            The GPG executable location is obtained by information in the windows registry.  Output itemType defaults to Ascii Armour.
            </remarks>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.Encrypt(System.IO.Stream,System.IO.Stream)">
            <summary>
            Encrypt OpenPGP data using IO Streams.
            </summary>
            <param name="inputStream">Input stream data containing the data to encrypt.</param>
            <param name="outputStream">Output stream which will contain encrypted data.</param>
            <remarks>
            You must add the recipient's public key to your GnuPG key ring before calling this method.  Please see the GnuPG documentation for more information.
            </remarks>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.Decrypt(System.IO.Stream,System.IO.Stream)">
            <summary>
            Decrypt OpenPGP data using IO Streams.
            </summary>
            <param name="inputStream">Input stream containing encrypted data.</param>
            <param name="outputStream">Output stream which will contain decrypted data.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.Sign(System.IO.Stream,System.IO.Stream)">
            <summary>
            Sign input stream data with default user key.
            </summary>
            <param name="inputStream">Input stream containing data to sign.</param>
            <param name="outputStream">Output stream containing signed data.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.Verify(System.IO.Stream)">
            <summary>
            Verify signed input stream data with default user key.
            </summary>
            <param name="inputStream">Input stream containing signed data to verify.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.GetSecretKeys">
            <summary>
            Retrieves a collection of secret keys from the GnuPG application.
            </summary>
            <returns>Collection of GnuPGKey objects.</returns>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.GetKeys">
            <summary>
            Retrieves a collection of all keys from the GnuPG application.
            </summary>
            <returns>Collection of GnuPGKey objects.</returns>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.Dispose">
            <summary>
            Dispose method for the GnuPG inteface class.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.Dispose(System.Boolean)">
            <summary>
            Dispose method for the GnuPG interface class.
            </summary>       
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.Finalize">
            <summary>
            Destructor method for the GnuPG interface class.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.CancelAsync">
            <summary>
            Cancels any asychronous operation that is currently active.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.EncryptAsync(System.IO.Stream,System.IO.Stream)">
            <summary>
            Starts asynchronous execution to encrypt OpenPGP data using IO Streams.
            </summary>
            <param name="inputStream">Input stream data containing the data to encrypt.</param>
            <param name="outputStream">Output stream which will contain encrypted data.</param>
            <remarks>
            You must add the recipient's public key to your GnuPG key ring before calling this method.  Please see the GnuPG documentation for more information.
            </remarks>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.DecryptAsync(System.IO.Stream,System.IO.Stream)">
            <summary>
            Starts asynchronous execution to decrypt OpenPGP data using IO Streams.
            </summary>
            <param name="inputStream">Input stream containing encrypted data.</param>
            <param name="outputStream">Output stream which will contain decrypted data.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPG.SignAsync(System.IO.Stream,System.IO.Stream)">
            <summary>
            Starts asynchronous execution to Sign OpenPGP data using IO Streams.
            </summary>
            <param name="inputStream">Input stream containing data to sign.</param>
            <param name="outputStream">Output stream which will contain Signed data.</param>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPG.Timeout">
            <summary>
            Get or set the timeout value for the GnuPG operations in milliseconds. 
            </summary>
            <remarks>
            The default timeout is 10000 milliseconds (10 seconds).
            </remarks>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPG.Recipient">
            <summary>
            Recipient name of the encrypted data.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPG.Passphrase">
            <summary>
            Secret passphrase text value.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPG.OutputType">
            <summary>
            The itemType of output that GPG should generate.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPG.HomePath">
            <summary>
            Path to your home directory.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPG.BinaryPath">
            <summary>
            Path to the location of the GPG.EXE binary executable.
            </summary>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPG.IsBusy">
            <summary>
            Gets a value indicating whether an asynchronous operation is running.
            </summary>
            <remarks>Returns true if an asynchronous operation is running; otherwise, false.
            </remarks>
        </member>
        <member name="P:Starksoft.Cryptography.OpenPGP.GnuPG.IsAsyncCancelled">
            <summary>
            Gets a value indicating whether an asynchronous operation is cancelled.
            </summary>
            <remarks>Returns true if an asynchronous operation is cancelled; otherwise, false.
            </remarks>
        </member>
        <member name="E:Starksoft.Cryptography.OpenPGP.GnuPG.EncryptAsyncCompleted">
            <summary>
            Event handler for EncryptAsync method completed.
            </summary>
        </member>
        <member name="E:Starksoft.Cryptography.OpenPGP.GnuPG.DecryptAsyncCompleted">
            <summary>
            Event handler for DecryptAsync completed.
            </summary>
        </member>
        <member name="E:Starksoft.Cryptography.OpenPGP.GnuPG.SignAsyncCompleted">
            <summary>
            Event handler for SignAsync completed.
            </summary>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.GnuPG.ActionTypes">
            <summary>
            GnuPG actions.
            </summary>
        </member>
        <member name="F:Starksoft.Cryptography.OpenPGP.GnuPG.ActionTypes.Encrypt">
            <summary>
            Encrypt data.
            </summary>
        </member>
        <member name="F:Starksoft.Cryptography.OpenPGP.GnuPG.ActionTypes.Decrypt">
            <summary>
            Decrypt data.
            </summary>
        </member>
        <member name="F:Starksoft.Cryptography.OpenPGP.GnuPG.ActionTypes.Sign">
            <summary>
            Sign data.
            </summary>
        </member>
        <member name="F:Starksoft.Cryptography.OpenPGP.GnuPG.ActionTypes.Verify">
            <summary>
            Verify signed data.
            </summary>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.SignAsyncCompletedEventArgs">
            <summary>
            Event arguments class for the SignAsyncCompleted event.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.SignAsyncCompletedEventArgs.#ctor(System.Exception,System.Boolean)">
            <summary>
            Constructor.
            </summary>
            <param name="error">Exception information generated by the event.</param>
            <param name="cancelled">Cancelled event flag.  This flag is set to true if the event was cancelled.</param>
        </member>
        <member name="T:Starksoft.Cryptography.OpenPGP.GnuPGBadPassphraseException">
            <summary>
            This exception is thrown when a bad passphrase is given resulting in an error condition when running the GPG.EXE program.   
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGBadPassphraseException.#ctor">
            <summary>
            Constructor.
            </summary>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGBadPassphraseException.#ctor(System.String)">
            <summary>
            Constructor.
            </summary>
            <param name="message">Exception message text.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGBadPassphraseException.#ctor(System.String,System.Exception)">
            <summary>
            Constructor.
            </summary>
            <param name="message">Exception message text.</param>
            <param name="innerException">The inner exception object.</param>
        </member>
        <member name="M:Starksoft.Cryptography.OpenPGP.GnuPGBadPassphraseException.#ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext)">
            <summary>
            Constructor.
            </summary>
            <param name="info">Serialization information.</param>
            <param name="context">Stream context information.</param>
        </member>
    </members>
</doc>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
A mid-level developer writing .NET code to provide business solutions to a wide range of customer requirements.

Comments and Discussions