using System;
using System.Collections.Generic;
using System.Security;
using GpgApi;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GpgInterface.ExePath = "path_to_gpg.exe";

            String fileName = "Path_to_file_to_encrypt";
            String encryptedFileName = "destination_path";

            // Produces a base64 file (can be opened in notepad for instance)
            Boolean armored = true;

            // Used only with asymetric algorithms (RSA)
            Boolean hideUserId = false;

            // If you want to sign the document with a sign key.
            KeyId keySignatureId = null;

            // If you want to encrypt using asymetric key, this is the list
            // of the public keys of the persons you want to encrypt to.
            List<KeyId> recipients = null;

            // The algorithm you want to use.
            CipherAlgorithm algorithm = CipherAlgorithm.Aes256;

            GpgEncrypt encrypt = new GpgEncrypt(fileName, encryptedFileName, armored, hideUserId, keySignatureId, recipients, algorithm);
            encrypt.AskPassphrase = GetPassword;

            {
                // The current thread is blocked until the encryption is finished.
                GpgInterfaceResult result = encrypt.Execute();
                Callback(result);
            }
            // or
            {
                encrypt.ExecuteAsync(Callback);
            }
        }

        public static SecureString GetPassword(AskPassphraseInfo info)
        {
            // Change this to return the password you want to use.
            return GpgInterface.GetSecureStringFromString("a");
        }

        public static void Callback(GpgInterfaceResult result)
        {
            if (result.Status == GpgInterfaceStatus.Success)
            {
                // ...
            }
            else
            {
                // ...
            }
        }
    }
}

Last edited Jun 20, 2013 at 8:02 AM by jimmygilles, version 3

Comments

demtron Feb 10, 2016 at 6:23 PM 
The program works except when I encrypt I get a popup asking for me to Enter passphrase. How can I get rid of this. I am trying to create an automated process without user intervention. I am using gpg.exe from https://www.gpg4win.org/

memomx Dec 15, 2015 at 2:35 PM 
Hi,
I have this code and it's working fine.

List<KeyId> recipients = new List<KeyId>();
recipients.Add(new KeyId("KeyIDstring"));

GpgEncrypt encrypt = new GpgEncrypt(fullPathFileName, encryptedFileName, armored, hideUserId, keySignatureId, recipients, CipherAlgorithm.Aes256);

archdome Nov 26, 2013 at 3:23 PM 
Hi, I'm having a problem when adding recipients to the encrypted data, it's returning me this error: "Exception of type 'GpgApi.InvalidKeyIdException' was thrown." and this is what I'm doing:

List<KeyId> lKey = new List<KeyId>();
lKey.Add(new KeyId("KeyId"));
_recipients = lKey;

The documentation isn't helpful about encryption using public Keys. So I ask you guys, how can I encrypt my data using a public Key with this code?

Best Regards