This project is read-only.

How to use GpgAPI ?


Using GpgAPI is easy... really :)

First, you must set the path to gpg.exe:
GpgInterface.ExePath = thepath;

Then you can set a SynchronizationContext, this is usefull when you develop an Wpf/Xaml or winform application:
GpgInterface.SynchronizationContext = SynchronizationContext.Current;

Now, you can use GpgAPI.
For example, you want to retrieve all the keys managed by gpg:

using System;
using GpgApi;

namespace Example
{
    public class gpgapi_example
    {
        public static void Main(String[] args)
        {
            GpgInterface.ExePath = args[0];

            GpgListPublicKeys publicKeys = new GpgListPublicKeys();
            publicKeys.Execute();

            foreach (Key key in publicKeys.Keys)
            {
                Console.WriteLine(key.Id);
            }
        }
    }
}


Sometimes, GpgAPI needs a password. Here is an example:

As you will see, we use the method "ExecuteAsync". ExecuteAsync is an asynchrone method. It starts a new thread.
We use the async method because the generation of a key may take minutes.
When the process is finished, the delegate method is called.

using System;
using GpgApi;

namespace Example
{
    public class gpgapi_example
    {
        public static SecureString GetPassword(AskPassphraseInfo info)
        {
            // You can display a popup or anything else.
            // For the example, password "a" is returned.
            // You must return a SecureString instead of a String.

            // In you don't want to manage a SecureString,
            // you can use this convenience method :
            // GpgInterface.GetSecureStringFromString

            // The SecureString must be marked as read-only.

            return GpgInterface.GetSecureStringFromString("a");
        }

        public static void Main(String[] args)
        {
            GpgInterface.ExePath = @"C:\Program Files (x86)\GNU\GnuPG\gpg.exe";

            Name name = new Name("My name");
            Email email = new Email("example@example.com");
            String comment = "A comment";
            KeyAlgorithm algorithm = KeyAlgorithm.RsaRsa;
            UInt32 size = 4096;
            DateTime expirationdate = DateTime.Now.AddYears(1);

            GpgGenerateKey generate = new GpgGenerateKey(name, email, comment, algorithm, size, expirationdate);
            generate.AskPassphrase = GetPassword;
            generate.ExecuteAsync(delegate(GpgInterfaceResult result)
            {
                // ...
                Console.WriteLine("Generated!");
                // ...
            });
        }
    }
}

Last edited Apr 3, 2013 at 6:44 PM by jimmygilles, version 16

Comments

pmakanga Feb 24 at 1:46 PM 
Hi guys, I am new in codeplex and this is my very first post, I have tried following the above GpgAPI without success, it is also not clear to me how we go about the gpg.exe, is there a way out of generating the keys without installing running the gpg4win and only applying the gpgapi.dll, any code snippets for key generation will be highly appreciated

kaappi Jun 3, 2015 at 11:32 AM 
jwashbourne: you probably already found out what gpg.exe is and where to find it, but for future reference I'll comment here what it is and where to find it. Gpg is GNU Privacy Guard, a program for encrypting and decrypting stuff with PGP (Pretty Good Privacy). You can download it from their site https://www.gnupg.org/.

jwashbourne Nov 19, 2014 at 1:29 PM 
Its not clear tome where do we get gpg.exe? is that a seperate program we just have to already have. thanks.