开发者

ASP.Net: "Invalid cryptographic message type" error when encrypting message

开发者 https://www.devze.com 2023-01-29 03:15 出处:网络
public byte[] DoEncrypt(string message, X509Certificate2 cryptCert) { byte[] signedBytes = new System.Text.UTF8Encoding().GetBytes(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消