C# 有没有一种方法可以在介质托管上对文档进行数字签名?

C# 有没有一种方法可以在介质托管上对文档进行数字签名?,c#,digital-signature,medium-trust,C#,Digital Signature,Medium Trust,托管服务器无法执行以下操作: SignedXml.ComputeSignature(); 我认为fromXML和toXML方法需要完全信任。但这是一个惊喜。现在不可能对任何文件进行数字签名 在网上搜索时,我发现: 以前有人用过这个或其他方法吗?这篇文章的作者基本上是通过将不同的部分组合在一起以获得一些工作代码来重新发明轮子。虽然他们的方法应该有效,你也可以自己发明一些类似的方法(在这里和那里使用一些代码,并尝试使其有效),但他们(在历史上)确认存在已修复的错误,我认为可能存在更多的错误 JF

托管服务器无法执行以下操作:

SignedXml.ComputeSignature();
我认为fromXML和toXML方法需要完全信任。但这是一个惊喜。现在不可能对任何文件进行数字签名

在网上搜索时,我发现:


以前有人用过这个或其他方法吗?

这篇文章的作者基本上是通过将不同的部分组合在一起以获得一些工作代码来重新发明轮子。虽然他们的方法应该有效,你也可以自己发明一些类似的方法(在这里和那里使用一些代码,并尝试使其有效),但他们(在历史上)确认存在已修复的错误,我认为可能存在更多的错误


JFYI:我们提供在有限的环境中工作的代码,因为我们所有的代码都是自己编写并包含在程序集中的

我最终能够使用安全API开发在线激活系统


没有直接的方法可用,但基本API可用于生成数字签名。

我知道这篇文章很老,但可能有人会发现它很有用: 该解决方案与ASP.NET 3.5在中等信任下协同工作:

    private XmlDocument GetSignedDoc(XmlDocument doc)
{
      X509Certificate2 certificate = null;
                    try
                    {
                        certificate = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + licenceFile, licenceFilePass, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

                        if (certificate == null)
                            throw new Exception("The certificate i

s null!!!");
                }
                catch (Exception ex)
                {
                    exception += "X509Certificate2 fail! Did not get certificate " + AppDomain.CurrentDomain.BaseDirectory + licenceFile;
                    exception += FormatException(ex);
                    goto SetError;
                }
            RSACryptoServiceProvider myRSASigner = null;

            try
            {
                myRSASigner = (RSACryptoServiceProvider)certificate.PrivateKey;

                if (myRSASigner == null)
                {
                    throw new Exception("No valid cert was found");
                }


                    doc = SignXmlFile(doc, myRSASigner);

           catch (Exception ex)
                {
                    exception += "SignXmlFile failed";
                    exception += FormatException(ex);
                    goto SetError;
                }
}


@请确保您已经单独检查了XMLBlackbox软件包的价格——这是相当合理的。我刚刚检查了EZRSA(您的CodeProject链接),它对我想要做的工作很好。但是,我必须添加对SHA256的支持,而这并没有提供。我已经将修改后的源代码贡献给了Paul Sanders(非常感谢他)。
private static XmlDocument SignXmlFile(XmlDocument doc, RSACryptoServiceProvider myRSA)
            {
                byte[] sign_this = Encoding.UTF8.GetBytes(doc.InnerXml);
                byte[] signature = myRSA.SignData(sign_this, new SHA1CryptoServiceProvider());
                string base64_string = Convert.ToBase64String(signature);

                XmlElement Signature = doc.CreateElement("Signature");
                Signature.AppendChild(doc.CreateTextNode(base64_string));
                doc.DocumentElement.AppendChild(doc.ImportNode(Signature, true));

                return doc;
            }