Example #1
0
        public static void Main(string[] args)
        {
            // Create some sample data
            byte[] bytedata = new byte[1024];
            for (int i = 0; i < bytedata.Length; i++)
            {
                bytedata[i] = (byte)(i % 256);
            }

            // Create memory based buffer for GPGME
            GpgmeMemoryData memdata = new GpgmeMemoryData();

            // write sample data into the GPGME memory based buffer
            Console.WriteLine("Bytes written: " + memdata.Write(bytedata, bytedata.Length));

            // Set the cursor to the beginning
            Console.WriteLine("Seek to begin: " + memdata.Seek(0, SeekOrigin.Begin));

            // Re-read the data into a tempory buffer
            byte[] tmp = new byte[bytedata.Length];
            Console.WriteLine("Bytes read: " + memdata.Read(tmp));

            // Create stream based buffer (CBS)
            MemoryStream    memstream  = new MemoryStream(tmp);
            GpgmeStreamData streamdata = new GpgmeStreamData(memstream);

            // ..
            Console.WriteLine("Bytes written: " + streamdata.Write(bytedata, bytedata.Length));
            Console.WriteLine("Seek to begin: " + streamdata.Seek(0, SeekOrigin.Begin));
            byte[] tmp2 = new byte[bytedata.Length];
            Console.WriteLine("Bytes read: " + streamdata.Read(tmp2));
        }
        static void Main(string[] args)
        {
            // Load PGP key from file
            String pgpkeytext = File.ReadAllText(@"daniel.pub");

            // Create GPGME context
            Context ctx = new Context();

            /* Windows / C# uses Unicode to encode text
             * (a string uses 2 bytes for each character).
             *
             * We need to convert the String into a byte array.
             * Since pgpkeytext contains only ASCII you can use
             * either ASCIIEncoding or UTF8Encoding.
             */
            UTF8Encoding encoder = new UTF8Encoding();

            byte[] keydata = encoder.GetBytes(pgpkeytext);

            // Create a memory stream ..
            MemoryStream memstream = new MemoryStream(keydata);
            // .. and use this stream for GPGME
            GpgmeStreamData keystream = new GpgmeStreamData(memstream);

            KeyStore     store = ctx.KeyStore;
            ImportResult rst   = store.Import(keystream);

            // Show the results
            Console.WriteLine("Keys imported {0}\n"
                              + "Keys skipped: {1}\n"
                              + "Keys unchanged: {2}\n"
                              + "Keys not imported: {3}\n",
                              rst.Imported,
                              rst.SkippedNewKeys,
                              rst.Unchanged,
                              rst.NotImported);

            if (rst.Imports != null)
            {
                Console.WriteLine("Found keys: ");
                foreach (ImportStatus status in rst.Imports)
                {
                    Console.WriteLine("Key fingerprint: {0}\n",
                                      status.Fpr);
                }
            }

            return;
        }