Example #1
0
        public void TestPgpMimeSignAndEncrypt()
        {
            var self       = new MailboxAddress("MimeKit UnitTests", "*****@*****.**");
            var recipients = new List <MailboxAddress> ();

            // encrypt to ourselves...
            recipients.Add(self);

            var cleartext = new TextPart("plain");

            cleartext.Text = "This is some cleartext that we'll end up encrypting...";

            using (var ctx = new DummyOpenPgpContext()) {
                var encrypted = MultipartEncrypted.Create(ctx, self, DigestAlgorithm.Sha1, recipients, cleartext);

                //using (var file = File.Create ("pgp-signed-encrypted.asc"))
                //	encrypted.WriteTo (file);

                DigitalSignatureCollection signatures;
                var decrypted = encrypted.Decrypt(ctx, out signatures);

                Assert.IsInstanceOfType(typeof(TextPart), decrypted, "Decrypted part is not the expected type.");
                Assert.AreEqual(cleartext.Text, ((TextPart)decrypted).Text, "Decrypted content is not the same as the original.");

                Assert.AreEqual(1, signatures.Count, "Verify returned an unexpected number of signatures.");
                foreach (var signature in signatures)
                {
                    try {
                        bool valid = signature.Verify();

                        Assert.IsTrue(valid, "Bad signature from {0}", signature.SignerCertificate.Email);
                    } catch (DigitalSignatureVerifyException ex) {
                        Assert.Fail("Failed to verify signature: {0}", ex);
                    }
                }
            }
        }
Example #2
0
        public void TestPgpMimeEncryption()
        {
            var self       = new MailboxAddress("MimeKit UnitTests", "*****@*****.**");
            var recipients = new List <MailboxAddress> ();

            // encrypt to ourselves...
            recipients.Add(self);

            var cleartext = new TextPart("plain");

            cleartext.Text = "This is some cleartext that we'll end up encrypting...";

            using (var ctx = new DummyOpenPgpContext()) {
                var encrypted = MultipartEncrypted.Create(ctx, recipients, cleartext);

                //using (var file = File.Create ("pgp-encrypted.asc"))
                //	encrypted.WriteTo (file);

                var decrypted = encrypted.Decrypt(ctx);

                Assert.IsInstanceOfType(typeof(TextPart), decrypted, "Decrypted part is not the expected type.");
                Assert.AreEqual(cleartext.Text, ((TextPart)decrypted).Text, "Decrypted content is not the same as the original.");
            }
        }