예제 #1
0
        /// <summary>
        /// Returns a list of all engines available for use with the given context.
        /// </summary>
        public static List <EngineInfo> GetEngines(GpgContext ctx)
        {
            var engineInfoPtr = GpgMeWrapper.gpgme_ctx_get_engine_info(ctx.Handle);
            var engineInfo    = Marshal.PtrToStructure <GpgMeEngineInfo>(engineInfoPtr);

            return(engineInfo.ToList());
        }
예제 #2
0
        private static void Test1()
        {
            var context   = GpgContext.CreateContext();
            var recipient = context.FindKey("dev");

            var reencFile = Environment.ExpandEnvironmentVariables(@"%userprofile%\.password-store\testpw-reenc.gpg");
            var file      = MemoryGpgBuffer.CreateFromFile(reencFile);
            var plaintext = new StreamReader(context.Decrypt(file)).ReadToEnd();

            Console.WriteLine(plaintext);
        }
예제 #3
0
        private static void Test2()
        {
            var helloWorld = "Hello World!";

            Console.WriteLine(helloWorld);

            var context = GpgContext.CreateContext();

            context.KeylistMode = context.KeylistMode | GpgKeylistMode.WithSecret;
            context.ArmorMode   = GpgArmorMode.On;

            // Print GPG keys
            var keys = context.FindKeys().ToArray();

            foreach (var key in keys)
            {
                Console.WriteLine(key.ToString());
                Console.WriteLine($"\t{key.Uids.First()}");
                foreach (var subkey in key.Subkeys)
                {
                    Console.WriteLine($"\t{subkey}");
                }
            }


            MemoryGpgBuffer helloBuffer         = MemoryGpgBuffer.CreateFromString(helloWorld);
            var             encryptedBuffer     = context.Encrypt(helloBuffer, keys[4]);
            var             encryptedByteBuffer = new byte[(int)encryptedBuffer.Length];

            encryptedBuffer.Read(encryptedByteBuffer, 0, (int)encryptedBuffer.Length);
            Console.WriteLine("========");
            Console.WriteLine(Encoding.UTF8.GetString(encryptedByteBuffer));
            Console.WriteLine("========");
            encryptedBuffer.Position = 0;
            var prop = new byte[2];

            using (var decrypted = context.Decrypt(encryptedBuffer))
            {
                var decryptedByteBuffer = new byte[decrypted.Length];
                encryptedBuffer.Position = 0;
                var decryptedBuffer = context.Decrypt(encryptedBuffer);
                decryptedBuffer.Read(decryptedByteBuffer, 0, decryptedByteBuffer.Length);
                Console.WriteLine(Encoding.UTF8.GetString(decryptedByteBuffer));
            }
        }
예제 #4
0
        /// <summary>
        /// Searches the keyring for all keys that match the given search parameters,
        /// and stops the search as soon as the first match is found.
        /// </summary>
        public static GpgKey FindKey(GpgContext ctx, string pattern, bool privateOnly)
        {
            ErrorHandler.Check(GpgMeWrapper.gpgme_op_keylist_start(ctx.Handle, pattern, privateOnly));
            // Grab the first key
            IntPtr keyPtr;
            var    result = GpgMeWrapper.gpgme_op_keylist_next(ctx.Handle, out keyPtr);

            if (result != GpgMeError.GPG_ERR_NO_ERROR)
            {
                return(null);
            }
            // If the operation succeeds, keyPtr will have been assigned a value.
            // Grab the GpgMeKey structure belonging to the pointer
            var key = Marshal.PtrToStructure <GpgMeKey>(keyPtr);

            // Turn the GpgMeKey into a GpgKey (resolves the subkeys and uids linked lists)
            return(key.ToGpgKey(keyPtr));
        }
예제 #5
0
        private static void Test0()
        {
            // Create a new GPG context
            var context = GpgContext.CreateContext();

            context.KeylistMode = context.KeylistMode | GpgKeylistMode.WithSecret;

            // Print GPG keys
            var keys = context.FindKeys().ToArray();

            foreach (var key in keys)
            {
                Console.WriteLine(key.ToString());
                Console.WriteLine($"\t{key.Uids.First()}");
                foreach (var subkey in key.Subkeys)
                {
                    Console.WriteLine($"\t{subkey}");
                }
            }

            var passwordFile = Environment.ExpandEnvironmentVariables(@"%userprofile%\.password-store\testpw.gpg");
            // Create a GPG data buffer for storing the ciphertext
            var inputBuffer = MemoryGpgBuffer.CreateFromFile(passwordFile);

            var content = new StreamReader(context.Decrypt(inputBuffer)).ReadToEnd();

            Console.WriteLine("Decryption output:");
            Console.WriteLine(content);

            var result = context.Encrypt(MemoryGpgBuffer.CreateFromString(content + "Re-Encrypted: True\n"), keys.Take(1));

            var reEncPasswordFile = Environment.ExpandEnvironmentVariables(@"%userprofile%\.password-store\testpw-reenc.gpg");

            using (var fs = File.OpenWrite(reEncPasswordFile))
            {
                result.CopyTo(fs);
            }
            Console.WriteLine("File was re-encrypted successfully.");
        }
예제 #6
0
        /// <summary>
        /// Searches the keyring for all keys that match the given search parameters.
        /// </summary>
        public static IEnumerable <GpgKey> FindKeys(GpgContext ctx, string pattern, bool privateOnly)
        {
            // Start the key listing operation
            ErrorHandler.Check(GpgMeWrapper.gpgme_op_keylist_start(ctx.Handle, pattern, privateOnly));

            // Grab the first key
            IntPtr keyPtr;
            var    result = GpgMeWrapper.gpgme_op_keylist_next(ctx.Handle, out keyPtr);

            while (result == GpgMeError.GPG_ERR_NO_ERROR)
            {
                // If the operation succeeds, keyPtr will have been assigned a value.
                // Grab the GpgMeKey structure belonging to the pointer
                var key = Marshal.PtrToStructure <GpgMeKey>(keyPtr);

                // Turn the GpgMeKey into a GpgKey (resolves the subkeys and uids linked lists)
                yield return(key.ToGpgKey(keyPtr));

                // Try grabbing the next key
                result = GpgMeWrapper.gpgme_op_keylist_next(ctx.Handle, out keyPtr);
            }
            // End the key listing operation
            ErrorHandler.Check(GpgMeWrapper.gpgme_op_keylist_end(ctx.Handle));
        }