The .NET Framework EnvelopedCms is built on top of the CAPI features of CryptMsg * Windows. CryptMsgOpenToEncode supports two methods of encoding recipients, one of which is conditionally compiled (although I could not determine when it is unavailable, I suspect that this is a Win9x vs NT4 / WinXP problem).
On a whim, I looked that you can flip to use a different encoding, and if that changes your result here. Turns out yes, this makes internally "useCms" the result in recipient encryption algorithm 1.2.840.113549.1.1.1.
Option 1) Use SubjectKeyIdentifier
If you interact with another system, as is the case, make sure that the certificate has an explicit SubjectKeyIdentifier extension before using this form of identification .. NET / Windows will be implicit if not explicit, and not all CMS implementations will match the recipient certificate in this case (e.g. OpenSSL).
You accomplished this by changing your CmsRecipient to
CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, cert);
Option 2) Add UnprotectedAttribute
EnvelopedCms allows you to add other metadata to an unencrypted message. Specifying any of these values ββforces the encryptor / encoder to use an alternative encoding.
Before calling Encrypt add
// Pkcs9DocumentName requires a non-empty string. // You can use any AsnEncodedData value, though. encryptedMessage.UnprotectedAttributes.Add(new Pkcs9DocumentName("a"));
Each of them worked in local testing.
bartonjs
source share