ASP.Net:加密消息时出现“加密消息类型无效”错误

时间:2010-12-03 02:00:55

标签: asp.net encryption cryptography

    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();
    }

当我的应用程序尝试加密消息时,它会抛出异常'encryptedMessage.RecipientInfos'抛出“System.Security.Cryptography.CryptographicException”类型的异常,文本“无效的加密消息类型”出现在“encryptedMessage”行中。加密(受体)“

1 个答案:

答案 0 :(得分:0)

如果您实际上正在加密签名数据(如变量名称所示),则不应使用ContentInfo的默认内容类型。相反,您应该使用允许您指定signedData内容类型的alternative constructor