ASP.Net: "Invalid cryptographic message type" error when encrypting message
public byte[] DoEncrypt(string message, X509Certificate2 cryptCert)
{
byte[] signedBytes = new System.Text.UTF8Encoding().GetBytes(message);
EnvelopedCms encryptedMessage = new EnvelopedCms(new ContentInfo(signedBytes));
CmsRecipientCollection recipients = new CmsRecipientCollection();
CmsRecipient recipient = new CmsRecipient(cryptCert);
recipients.Add(recipient);
encryptedMessage.Encrypt(recipient);
return encryptedMessage.Encode();
}
When my application tries开发者_如何学JAVA to encrypt a message it throws the exception 'encryptedMessage.RecipientInfos' threw an exception of type 'System.Security.Cryptography.CryptographicException' with the text "Invalid cryptographic message type" occurs in the line "encryptedMessage.Encrypt(recipient)"
If you are actually encrypting signed data (as the variable names suggests), you should not use the default content type for your ContentInfo. Instead you should use the alternative constructor that allows you to specify the signedData content type.
精彩评论