XML DSIG:.NET 中的封装签名转换
我正在使用 XmlDsigEnvelopedSignatureTransform 使用 RSA 私钥对 XML 文件进行数字签名。
然而,我真正想要的是使用“封装”签名来签署 xml。 .NET 对此有本机支持吗?
顺便说一下,这是我的代码:
public static void SignXml(XmlDocument xmlDoc, RSA key)
{
// Check arguments.
if (xmlDoc == null)
throw new ArgumentException("xmlDoc");
if (key == null)
throw new ArgumentException("Key");
SignedXml xml = new SignedXml(xmlDoc);
xml.SigningKey = key;
Reference reference = new Reference();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(true);
reference.AddTransform(env);
xml.AddReference(reference);
xml.ComputeSignature();
XmlElement element = xml.GetXml();
MessageBox.Show(element.OuterXml);
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(element, true));
}
I'm using the XmlDsigEnvelopedSignatureTransform to digitally sign an XML file using an RSA private key.
However, what I really want is to sign the xml using an "enveloping" signature. Does .NET have native support for that?
By the way, here's my code:
public static void SignXml(XmlDocument xmlDoc, RSA key)
{
// Check arguments.
if (xmlDoc == null)
throw new ArgumentException("xmlDoc");
if (key == null)
throw new ArgumentException("Key");
SignedXml xml = new SignedXml(xmlDoc);
xml.SigningKey = key;
Reference reference = new Reference();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform(true);
reference.AddTransform(env);
xml.AddReference(reference);
xml.ComputeSignature();
XmlElement element = xml.GetXml();
MessageBox.Show(element.OuterXml);
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(element, true));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的签名不是签名数据的子元素,则不需要封装签名转换。
因此,只需跳过 XmlDsigEnvelopedSignatureTransform 并按照其他方式执行签名即可:
If your signature is not a sub-element of the signed data you do not need the Enveloped Signature Transform.
So just skip the
XmlDsigEnvelopedSignatureTransform
and perform the signature as you would otherwise: