MimeKit.MimeMessage.SignAndEncrypt C# (CSharp) Method

SignAndEncrypt() public method

Sign and encrypt the message to the sender and all of the recipients using the specified cryptography context and the SHA-1 digest algorithm.

If either of the Resent-Sender or Resent-From headers are set, then the message will be signed using the Resent-Sender (or first mailbox in the Resent-From) address as the signer address, otherwise the Sender or From address will be used instead.

Likewise, if either of the Resent-Sender or Resent-From headers are set, then the message will be encrypted to all of the addresses specified in the Resent headers (Resent-Sender, Resent-From, Resent-To, Resent-Cc, and Resent-Bcc), otherwise the message will be encrypted to all of the addresses specified in the standard address headers (Sender, From, To, Cc, and Bcc).

/// is null. /// /// An unknown type of cryptography context was used. /// /// The has not been set. /// -or- /// The sender has been specified. /// -or- /// No recipients have been specified. /// /// A certificate could not be found for the signer or one or more of the recipients. /// /// The private key could not be found for the sender. /// /// The public key could not be found for one or more of the recipients. ///
public SignAndEncrypt ( MimeKit.Cryptography.CryptographyContext ctx ) : void
ctx MimeKit.Cryptography.CryptographyContext The cryptography context.
return void
		public void SignAndEncrypt (CryptographyContext ctx)
		{
			SignAndEncrypt (ctx, DigestAlgorithm.Sha1);
		}
#endif // ENABLE_CRYPTO

Same methods

MimeMessage::SignAndEncrypt ( MimeKit.Cryptography.CryptographyContext ctx, DigestAlgorithm digestAlgo ) : void

Usage Example

Exemplo n.º 1
0
		public void TestMimeMessageSignAndEncrypt ()
		{
			var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up signing and encrypting..." };
			var self = new SecureMailboxAddress ("MimeKit UnitTests", "*****@*****.**", "AB0821A2");
			var message = new MimeMessage { Subject = "Test of signing with OpenPGP" };
			DigitalSignatureCollection signatures;

			message.From.Add (self);
			message.To.Add (self);
			message.Body = body;

			using (var ctx = new DummyOpenPgpContext ()) {
				message.SignAndEncrypt (ctx);

				Assert.IsInstanceOf<MultipartEncrypted> (message.Body);

				var encrypted = (MultipartEncrypted) message.Body;

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

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

				Assert.IsInstanceOf<TextPart> (decrypted, "Decrypted part is not the expected type.");
				Assert.AreEqual (body.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);
					}
				}
			}
		}
All Usage Examples Of MimeKit.MimeMessage::SignAndEncrypt