我们使用BizTalk解决方案来签名并发送邮件标题,问题是根据收件人的签名无效,此收件人有一个JAVA研讨会。
今天的现有代码是用Java编写的,有效,我们希望将其迁移到C#代码
Bellow是使用JAVA代码签名的邮件标头之一,并且可以正常工作,检查DigestValue
以下是在C#代码中签名的相同标头不起作用,因为您可以看到相同的DigestValue但不同的SignatureValue。 我能看到的唯一区别是,在有效的签名中,证书每76个字符都有回车。
以下是我用于签名标题的C#代码,证书全局分配
使用 PreserveWhitespace = true 创建xmlDoc;
private void SignXml(XmlDocument xmlDoc, string referenceURI)
{
// Check arguments.
if (xmlDoc == null)
throw new ArgumentException("xmlDoc");
if (certificate == null)
throw new ArgumentException("Key");
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(xmlDoc);
// Add the key to the SignedXml document.
signedXml.SigningKey = certificate.PrivateKey;
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;//NEW
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = String.Format("#{0}", referenceURI);
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
XmlDsigExcC14NTransform c14trf = new XmlDsigExcC14NTransform(false, "xs");
//might need some InclusiveNamespaces
c14trf.Algorithm = SignedXml.XmlDsigExcC14NTransformUrl;
reference.AddTransform(c14trf);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
//AddKeyInfo value, optional in standard
KeyInfo keyInfo = new KeyInfo();
KeyInfoX509Data keyinfoData = new KeyInfoX509Data(certificate);
keyInfo.AddClause(keyinfoData);
signedXml.KeyInfo = keyInfo;
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
if (signedXml.CheckSignature(certificate, true) == false)
{
throw new ArgumentException("INT0014a Signature is incorrect", "CheckSignature");
}
// Append the element to the XML document.
xmlDoc.DocumentElement.InsertAfter(xmlDoc.ImportNode(xmlDigitalSignature,true), xmlDoc.DocumentElement.FirstChild);
}
感谢任何帮助