XML使用SHA-256签名 [英] XML Signed withSHA-256

查看:498
本文介绍了XML使用SHA-256签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要数字签名XML文件(SHA-256)

i am trying to digitaliy sign XML File (SHA-256)

static void Main(string[] args)
    {

        XmlTextReader reader = new XmlTextReader(@"F:\dev.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
        string pfxpath = @"F:\dec.pfx";
        X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxpath), "Pass", X509KeyStorageFlags.Exportable);
        signxml(doc, cert);
        File.WriteAllText(@"F:\a2.xml", doc.OuterXml);
    }

这是我的功能

public static void signxml(XmlDocument doc, X509Certificate2 cert)
    {

        SignedXml signxm = new SignedXml(doc);
        signxm.SigningKey = cert.PrivateKey;
        signxm.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
        Reference refrence = new Reference();
        refrence.Uri="";
        refrence.AddTransform(new XmlDsigEnvelopedSignatureTransform());
        signxm.AddReference(refrence);
        KeyInfo key = new KeyInfo();
        key.AddClause(new KeyInfoX509Data(cert));
        signxm.KeyInfo = key;
        signxm.ComputeSignature();

        XmlElement xmlsig = signxm.GetXml();
        doc.DocumentElement.AppendChild(doc.ImportNode(xmlsig, true));
    }

在signxm.ComputeSignature()中有一个错误
;
,这是描述{无法为所提供的签名算法创建签名描述。}
那么如何解决呢?

there is an error in signxm.ComputeSignature(); and this is the description {"SignatureDescription could not be created for the signature algorithm supplied."} So How To Solve it ??

推荐答案

标准.NET框架不知道如何翻译 http://www.w3.org/2001/04/xmldsig-more #rsa-sha256 到算法。

Standard .NET framework does not know how to translate http://www.w3.org/2001/04/xmldsig-more#rsa-sha256 to algorithm.

要解决此问题,您需要 Security.Cryptography.dll 。按照本指南操作。您必须

To solve this you need Security.Cryptography.dll. Follow this guide. You will have to


  1. 在.NET machine.config中注册算法

  2. 添加Security.Cryptography .dll到GAC(但它工作,如果我直接在我的项目中引用它)

这篇关于XML使用SHA-256签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆