在.Net中使用ECDsa和SHA256签署XML

时间:2014-12-11 10:37:57

标签: c# cryptography ecdsa cng signedxml

我正在尝试在.Net中使用带有ECDsa(ECDsaCng)和SHA256的SignedXml,并且我收到“CryptographicException”,并显示消息“无法创建签名密钥”。有人知道如何实现这一目标,或者我是否应该知道有关ecdsa .net支持的相关信息?提前谢谢你。

public XmlDocument SignXml(XmlDocument xmlDoc)
    {
        CngKey cngKey = CngKey.Create(CngAlgorithm.ECDsaP256);
        ecDsaCng = new ECDsaCng(cngKey);
        ecDsaCng.HashAlgorithm = CngAlgorithm.ECDsaP256;
        ecDsaCng.KeySize = 256;

        xmlDoc.PreserveWhitespace = true;

        SignedXml signedXml = new SignedXml(xmlDoc);
        signedXml.SigningKey = ecDsaCng;

        Reference reference = new Reference();
        reference.Uri = "";
        XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
        reference.AddTransform(env);
        signedXml.AddReference(reference);

        signedXml.ComputeSignature();

        XmlElement xmlDigitalSignature = signedXml.GetXml();
        xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
        return xmlDoc;
    }

“signedXml.ComputeSignature();”中出现错误方法调用。

1 个答案:

答案 0 :(得分:1)

查看程序集

  

[System.Security,Version = 4.0.0.0,Culture = neutral,   公钥= b03f5f7f11d50a3a]

方法

  

public void ComputeSignature()

仅接受 RSA DSA 键。遗憾的是它没有在MSDN的某处指定。您必须传递SignedXml类。