public MimeEntity Decrypt (out DigitalSignatureCollection signatures)
{
var protocol = ContentType.Parameters["protocol"];
if (string.IsNullOrEmpty (protocol))
throw new FormatException ();
protocol = protocol.Trim ().ToLowerInvariant ();
if (Count < 2)
throw new FormatException ();
var version = this[0] as MimePart;
if (version == null)
throw new FormatException ();
var ctype = version.ContentType;
var value = string.Format ("{0}/{1}", ctype.MediaType, ctype.MediaSubtype);
if (value.ToLowerInvariant () != protocol)
throw new FormatException ();
var encrypted = this[1] as MimePart;
if (encrypted == null || encrypted.ContentObject == null)
throw new FormatException ();
if (!encrypted.ContentType.IsMimeType ("application", "octet-stream"))
throw new FormatException ();
using (var ctx = CryptographyContext.Create (protocol)) {
using (var memory = new MemoryBlockStream ()) {
var pgp = ctx as OpenPgpContext;
encrypted.ContentObject.DecodeTo (memory);
memory.Position = 0;
if (pgp != null)
return pgp.Decrypt (memory, out signatures);
signatures = null;
return ctx.Decrypt (memory);
}
}
}