Mahmoud Mahmoud - 1 year ago 378
C# Question

.Net: SignedXml - Signing xml with transform algorithm exc-c14n

I'm trying to sign Xml (actually SOAP xml) in C# using the SignedXml class, the signing stage passes successfully, but when I try to verify the signature it tells me that it is not valid.
The only change I have done from the example in MSDN that I used XmlDsigExcC14NTransform instead of the transform XmlDsigEnvelopedSignatureTransform.
If I use XmlDsigEnvelopedSignatureTransform I will get a valid signature.

Here is my signing code:

private static XmlDocument SignXml(XmlDocument doc)
SignedXml signedXml = new SignedXml(doc);
signedXml.SigningKey = Certificate.PrivateKey;

Reference reference = new Reference();
reference.Uri = "";

XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
//XmlDsigExcC14NTransform env = new XmlDsigExcC14NTransform();



XmlElement signature = signedXml.GetXml();
return doc;

The above code will give me a valid signature, but if I use

XmlDsigExcC14NTransform env = new XmlDsigExcC14NTransform();

instead of

XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();

I will get an invalid signature.

Here is my verification code:

private static bool Verify(XmlDocument doc)
SignedXml signedDoc = new SignedXml(doc);
XmlNodeList nodeList = doc.GetElementsByTagName(Constants.SignatureElement);

return signedDoc.CheckSignature((RSA)Certificate.PublicKey.Key);

Can anyone tell me how can I sign with transform algorithm of

Thanks in advance.

Answer Source

You are gonna need the XmlDsigEnvelopedSignatureTransform in your case because you are adding the signature inside the element you are signing.

XmlDsigEnvelopedSignatureTransform will tell the SignedXml class to remove the signature from the signature node itself before testing it's validity. This is needed because you added that element after calculating the signature.

You can add more then one transform by calling the AddTransform again like this:

XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
XmlDsigExcC14NTransform c14n = new XmlDsigExcC14NTransform();


However i think what you actually want to do instead of my example above is set the CanonicalizationMethod to c14n:

signedXml.SignedInfo.CanonicalizationMethod = "";
 - or - 
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download