当前位置: 首页>>代码示例>>C#>>正文


C# Context.SetEngineInfo方法代码示例

本文整理汇总了C#中Context.SetEngineInfo方法的典型用法代码示例。如果您正苦于以下问题:C# Context.SetEngineInfo方法的具体用法?C# Context.SetEngineInfo怎么用?C# Context.SetEngineInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Context的用法示例。


在下文中一共展示了Context.SetEngineInfo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            Context ctx = new Context();

            if (ctx.Protocol != Protocol.OpenPGP)
                ctx.SetEngineInfo(Protocol.OpenPGP, null, null);

            Console.WriteLine("Search Bob's PGP key in the default keyring..");

            String searchstr = "[email protected]";
            IKeyStore keyring = ctx.KeyStore;

            // retrieve all keys that have Bob's email address
            Key[] keys = keyring.GetKeyList(searchstr, false);
            if (keys == null || keys.Length == 0)
            {
                Console.WriteLine("Cannot find Bob's PGP key {0} in your keyring.", searchstr);
                Console.WriteLine("You may want to create the PGP key by using the appropriate\n"
                    + "sample in the Samples/ directory.");
                return;
            }

            // print a list of all returned keys
            foreach (Key key in keys)
                if (key.Uid != null
                    && key.Fingerprint != null)
                    Console.WriteLine("Found key {0} with fingerprint {1}",
                        key.Uid.Name,
                        key.Fingerprint);

            // we are going to use the first key in the list
            PgpKey bob = (PgpKey)keys[0];
            if (bob.Uid == null || bob.Fingerprint == null)
                throw new InvalidKeyException();

            // Create a sample string
            StringBuilder randomtext = new StringBuilder();
            for (int i = 0; i < 80 * 6; i++)
                randomtext.Append((char)(34 + i%221));
            string secrettext = new string('+', 508)
                + " Die Gedanken sind frei "
                + new string('+', 508)
                + randomtext.ToString();

            Console.WriteLine("Text to be encrypted:\n\n{0}", secrettext);

            // we want our string UTF8 encoded.
            UTF8Encoding utf8 = new UTF8Encoding();

            // create a (dynamic) memory based data buffer to place the unencrypted (plain) text
            GpgmeData plain = new GpgmeMemoryData();

            // set a filename for this data buffer
            plain.FileName = "my_document.txt";

            BinaryWriter binwriter = new BinaryWriter(plain, utf8);

            // write our secret text to the memory buffer
            binwriter.Write(secrettext.ToCharArray());
            binwriter.Flush();
            // go to the beginning(!)
            binwriter.Seek(0, SeekOrigin.Begin);

            /////// ENCRYPT DATA ///////

            // we want or PGP encrypted data RADIX/BASE64 encoded.
            ctx.Armor = true;

            // create another (dynamic) memory based data buffer as destination
            GpgmeData cipher = new GpgmeMemoryData();
            cipher.FileName = "my_document.txt";

            Console.Write("Encrypt data for {0} ({1}).. ",
                bob.Uid.Name, bob.KeyId);

            EncryptionResult encrst = ctx.Encrypt(
                new Key[] { bob },          // encrypt data to Bob's key only
                EncryptFlags.AlwaysTrust,   // trust our sample PGP key
                plain,      // source buffer
                cipher);    // destination buffer

            Console.WriteLine("done.");
            Console.WriteLine("Cipher text:");

            // move cursor to the beginning
            cipher.Seek(0, SeekOrigin.Begin);

            /* Read cipher text from libgpgme's memory based buffer and print
             * it to the console screen.
             */
            char[] buf;
            // the cipher text is UTF8 encoded
            BinaryReader binreader = new BinaryReader(cipher, utf8);
            while (true)
            {
                try
                {
                    buf = binreader.ReadChars(255);
                    if (buf.Length == 0)
                        break;
//.........这里部分代码省略.........
开发者ID:anonghosteam,项目名称:outlook-privacy-plugin,代码行数:101,代码来源:Program.cs


注:本文中的Context.SetEngineInfo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。