using System.Collections.Generic;
public static void Main()
Console.Write("Generating keys...");
using var keyGenerator = CloneKeyStreams().GetEnumerator();
Console.WriteLine("DONE\n");
string secretContentToEncrypt = "This is a super secret message!!!";
Console.WriteLine("Secret message to send: {0}\n", secretContentToEncrypt);
var (alicePublicKey, alicePrivateKey, bobPublicKey, bobPrivateKey, bobNewPublicKey, bobNewPrivateKey) = keyGenerator.Current;
EncryptionKeys encryptionKeys = new EncryptionKeys(new [] { bobPublicKey, bobNewPublicKey }, alicePrivateKey, "blah");
PGP pgp = new PGP(encryptionKeys);
string encryptedSignedContent = pgp.EncryptArmoredStringAndSign(secretContentToEncrypt);
Console.WriteLine("Encrypted and signed content from Alice:\n\n{0}", encryptedSignedContent);
(alicePublicKey, alicePrivateKey, bobPublicKey, bobPrivateKey, bobNewPublicKey, bobNewPrivateKey) = keyGenerator.Current;
encryptionKeys = new EncryptionKeys(alicePublicKey, bobPrivateKey, "blah");
pgp = new PGP(encryptionKeys);
var decryptedByBob = pgp.DecryptArmoredStringAndVerify(encryptedSignedContent);
Console.WriteLine("Decrypted with bob's old key: {0}", decryptedByBob);
(alicePublicKey, alicePrivateKey, bobPublicKey, bobPrivateKey, bobNewPublicKey, bobNewPrivateKey) = keyGenerator.Current;
encryptionKeys = new EncryptionKeys(alicePublicKey, bobNewPrivateKey, "blah");
pgp = new PGP(encryptionKeys);
var decryptedByBobNew = pgp.DecryptArmoredStringAndVerify(encryptedSignedContent);
Console.WriteLine("Decrypted with bob's new key: {0}", decryptedByBob);
IEnumerable<(Stream apu, Stream apr, Stream bpu, Stream bpr, Stream bnpu, Stream bnpr)> CloneKeyStreams()
using var alicePublicKey = new MemoryStream();
using var alicePrivateKey = new MemoryStream();
new PGP().GenerateKey(alicePublicKey, alicePrivateKey, "foo", "blah");
using var bobPublicKey = new MemoryStream();
using var bobPrivateKey = new MemoryStream();
new PGP().GenerateKey(bobPublicKey, bobPrivateKey, "foo", "blah");
using var bobNewPublicKey = new MemoryStream();
using var bobNewPrivateKey = new MemoryStream();
new PGP().GenerateKey(bobNewPublicKey, bobNewPrivateKey, "foo", "blah");
ResetPositions(alicePublicKey, alicePrivateKey, bobPublicKey, bobPrivateKey, bobNewPublicKey, bobNewPrivateKey);
var apu = new MemoryStream();
var apr = new MemoryStream();
var bpu = new MemoryStream();
var bpr = new MemoryStream();
var bnpu = new MemoryStream();
var bnpr = new MemoryStream();
alicePublicKey.CopyTo(apu);
alicePrivateKey.CopyTo(apr);
bobPublicKey.CopyTo(bpu);
bobPrivateKey.CopyTo(bpr);
bobNewPublicKey.CopyTo(bnpu);
bobNewPrivateKey.CopyTo(bnpr);
ResetPositions(apu, apr, bpu, bpr, bnpu, bnpr);
yield return (apu, apr, bpu, bpr, bnpu, bnpr);
void ResetPositions(params Stream[] args)
foreach(var s in args) s.Position = 0;