public SignAndEncrypt ( MimeKit.Cryptography.CryptographyContext ctx ) : void | ||
ctx | MimeKit.Cryptography.CryptographyContext | The cryptography context. |
return | void |
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); } } } }
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", "*****@*****.**", "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"); } } }