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

标签 c# cryptography ecdsa cng signedxml

我尝试在 .Net 中将 SignedXml 与 ECDsa (ECDsaCng) 和 SHA256 结合使用,但收到“CryptographicException”,其中包含消息“无法创建签名 key 。”。有人知道如何实现该目标,或者我是否应该了解有关 .net 对 ecdsa 支持的相关信息?预先感谢您。

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();”中出现错误方法调用。

最佳答案

看看程序集

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

在方法中

public void ComputeSignature()

仅接受 RSADSA key 。遗憾的是,MSDN 上没有指定它。 。您必须传递 SignedXml 类。

关于c# - 在 .Net 中使用 ECDsa 和 SHA256 签署 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420789/

相关文章:

c# - Azure 表存储 SaveChanges 返回 "The requested operation is not implemented on the specified resource."

c# - 访问不在基础接口(interface)上的方法

c# - EF Core RC2 最终版本 : No database provider has been configured for this DbContext

java - PKCS#11 实例化问题

c# - Diffie-Hellman key 交换 - 澄清?

c# - 在 C# 中使用 ECDSA 生成证书

c# - 密码学 : Turning a String array into a single string

java - 读取和写入 java KeyStore

java - 从 PKCS8 编码数据创建任何 PrivateKey 实例(RSA 或 DSA 或 EC)

go - 无法使用 Go X509 pkg 解析 ECDSA key