本文整理汇总了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;
//.........这里部分代码省略.........