简体   繁体   English

如何使用Mimekit检查MIME消息是否已加密或签名?

[英]How to check if a MIME message is encrypted or signed using Mimekit?

I want to JUST check if a MIME message is encrypted or signed using Mimekit. 我想只是检查MIME消息是否使用Mimekit加密或签名。

I don't want to decrypt it or verify its signature, I just want to know if it is encrypted or signed. 我不想解密它或验证它的签名,我只是想知道它是加密还是签名。

I expected to find this as functions: IsSigned() & IsEncrypted() in the MimeMessage Class but these functions don't exist! 我希望找到它作为函数:MimeMessage类中的IsSigned()和IsEncrypted()但这些函数不存在!

Thank you. 谢谢。

Those methods don't exist because a MimeMessage itself cannot be signed or encrypted, only the Body (or a subset of the body) of the message can be signed or encrypted. 这些方法不存在,因为MimeMessage本身不能被签名或加密,只能对消息的Body (或正文的一个子集)进行签名或加密。

A very simple solution is to do this: 一个非常简单的解决方案是:

var pkcs7 = message.Body as ApplicationPkcs7Mime;

bool signed = (message.Body is MultipartSigned) ||
    (pkcs7 != null && pkcs7.SecureMimeType == SecureMimeType.SignedData);

bool encrypted = (message.Body is MultipartEncrypted) ||
    (pkcs7 != null && pkcs7.SecureMimeType == SecureMimeType.EnvelopedData);

Note: MultipartEncrypted is only used with PGP (not S/MIME), so if you only care about S/MIME, you do not need to check for MultipartEncrypted . 注意: MultipartEncrypted仅用于PGP(不是S / MIME),因此如果您只关心S / MIME,则无需检查MultipartEncrypted

Likewise, ApplicationPkcs7Mime is only used for S/MIME, so if you only care about PGP, you do not need to check for ApplicationPkcs7Mime . 同样, ApplicationPkcs7Mime仅用于S / MIME,因此如果您只关心PGP,则无需检查ApplicationPkcs7Mime

Both PGP and S/MIME (can) use MultipartSigned , though, so you'll have to check that in either case. 但是,PGP和S / MIME(都可以)都使用MultipartSigned ,所以在任何一种情况下你都必须检查它。

Since every client I've ever used or received mail from only ever signs and/or encrypts the top-level Body part of the message, the above check is likely all you'll need. 因为每一个客户,我曾经使用或只有永远的迹象收到的邮件和/或加密顶级Body的消息的一部分,上述检查很可能所有你需要的。 However, it is possible for MIME-compliant clients to sign and/or encrypt a subpart of the Body , so you'd have to traverse the MIME tree and check each node if you want to be 100% compliant. 但是,它是可能的MIME兼容客户端签名和/或加密的子部分Body ,所以你必须遍历MIME树和检查每个节点,如果你想成为标准的100%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM