本文整理汇总了C#中MimeKit.MimeMessage.SignAndEncrypt方法的典型用法代码示例。如果您正苦于以下问题:C# MimeMessage.SignAndEncrypt方法的具体用法?C# MimeMessage.SignAndEncrypt怎么用?C# MimeMessage.SignAndEncrypt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MimeKit.MimeMessage
的用法示例。
在下文中一共展示了MimeMessage.SignAndEncrypt方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestMimeMessageSignAndEncrypt
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", "[email protected]", "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);
}
}
}
}
示例2: TestSecureMimeSignAndEncrypt
public void TestSecureMimeSignAndEncrypt ()
{
var body = new TextPart ("plain") { Text = "This is some cleartext that we'll end up signing and encrypting..." };
var self = new SecureMailboxAddress ("MimeKit UnitTests", "[email protected]", "b7dd33847c3308dd9e12b4c3c94b545d76ab5e41");
var message = new MimeMessage { Subject = "Test of signing and encrypting with S/MIME" };
ApplicationPkcs7Mime encrypted;
message.From.Add (self);
message.To.Add (self);
message.Body = body;
using (var ctx = CreateContext ()) {
message.SignAndEncrypt (ctx);
Assert.IsInstanceOf<ApplicationPkcs7Mime> (message.Body, "The message body should be an application/pkcs7-mime part.");
encrypted = (ApplicationPkcs7Mime) message.Body;
Assert.AreEqual (SecureMimeType.EnvelopedData, encrypted.SecureMimeType, "S/MIME type did not match.");
}
using (var ctx = CreateContext ()) {
var decrypted = encrypted.Decrypt (ctx);
// The decrypted part should be a multipart/signed
Assert.IsInstanceOf<MultipartSigned> (decrypted, "Expected the decrypted part to be a multipart/signed.");
var signed = (MultipartSigned) decrypted;
Assert.IsInstanceOf<TextPart> (signed[0], "Expected the first part of the multipart/signed to be a multipart.");
Assert.IsInstanceOf<ApplicationPkcs7Signature> (signed[1], "Expected second part of the multipart/signed to be a pkcs7-signature.");
var extracted = (TextPart) signed[0];
Assert.AreEqual (body.Text, extracted.Text, "The decrypted text part's text does not match the original.");
var signatures = signed.Verify (ctx);
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);
}
var algorithms = ((SecureMimeDigitalSignature) signature).EncryptionAlgorithms;
Assert.AreEqual (EncryptionAlgorithm.Camellia256, algorithms[0], "Expected Camellia-256 capability");
Assert.AreEqual (EncryptionAlgorithm.Aes256, algorithms[1], "Expected AES-256 capability");
Assert.AreEqual (EncryptionAlgorithm.Camellia192, algorithms[2], "Expected Camellia-192 capability");
Assert.AreEqual (EncryptionAlgorithm.Aes192, algorithms[3], "Expected AES-192 capability");
Assert.AreEqual (EncryptionAlgorithm.Camellia128, algorithms[4], "Expected Camellia-128 capability");
Assert.AreEqual (EncryptionAlgorithm.Aes128, algorithms[5], "Expected AES-128 capability");
Assert.AreEqual (EncryptionAlgorithm.Idea, algorithms[6], "Expected IDEA capability");
Assert.AreEqual (EncryptionAlgorithm.Cast5, algorithms[7], "Expected Cast5 capability");
Assert.AreEqual (EncryptionAlgorithm.TripleDes, algorithms[8], "Expected Triple-DES capability");
//Assert.AreEqual (EncryptionAlgorithm.RC2128, algorithms[9], "Expected RC2-128 capability");
//Assert.AreEqual (EncryptionAlgorithm.RC264, algorithms[10], "Expected RC2-64 capability");
//Assert.AreEqual (EncryptionAlgorithm.Des, algorithms[11], "Expected DES capability");
//Assert.AreEqual (EncryptionAlgorithm.RC240, algorithms[12], "Expected RC2-40 capability");
}
}
}