A
AK
Hello,
I'm trying to create an XML digital signature over some assets listed
in the XML file. It works fine until I add a pdf file in the list of
assets, which throws an exception saying that "Data at the root level
is invalid". I'm sure it must be possible to sign binary files as
well, but I've been searching for hours and can't find any examples.
Hope someone can help me with this.
Here's the code I'm using:
// Create a SignedXml object
SignedXml signedXml = new SignedXml(Doc);
// Add the key
signedXml.SigningKey = Key;
// Get urls to assets with signed = true
assetUris = getAssetUris();
foreach (string assetUri in assetUris)
{
// Create a reference to be signed
Reference reference = new Reference();
reference.Uri = assetUri;
// Add an enveloped transformation to the reference
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object
signedXml.AddReference(reference);
}
// Hash the entire doc as well so that the asset description will be
hashed
Reference reference2 = new Reference();
reference2.Uri = "";
signedXml.AddReference(reference2);
// Add an enveloped transformation to the reference
XmlDsigEnvelopedSignatureTransform env2 = new
XmlDsigEnvelopedSignatureTransform();
reference2.AddTransform(env2);
// Add the KeyInfo object holding the certificate into the SignedXml
object
signedXml.KeyInfo = getKeyInfo();
// Compute the signature
signedXml.ComputeSignature();
// Get the XML representation of the signature and save it to an
XmlElement object
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document
Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature,
true));
Many thanks,
AK
I'm trying to create an XML digital signature over some assets listed
in the XML file. It works fine until I add a pdf file in the list of
assets, which throws an exception saying that "Data at the root level
is invalid". I'm sure it must be possible to sign binary files as
well, but I've been searching for hours and can't find any examples.
Hope someone can help me with this.
Here's the code I'm using:
// Create a SignedXml object
SignedXml signedXml = new SignedXml(Doc);
// Add the key
signedXml.SigningKey = Key;
// Get urls to assets with signed = true
assetUris = getAssetUris();
foreach (string assetUri in assetUris)
{
// Create a reference to be signed
Reference reference = new Reference();
reference.Uri = assetUri;
// Add an enveloped transformation to the reference
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object
signedXml.AddReference(reference);
}
// Hash the entire doc as well so that the asset description will be
hashed
Reference reference2 = new Reference();
reference2.Uri = "";
signedXml.AddReference(reference2);
// Add an enveloped transformation to the reference
XmlDsigEnvelopedSignatureTransform env2 = new
XmlDsigEnvelopedSignatureTransform();
reference2.AddTransform(env2);
// Add the KeyInfo object holding the certificate into the SignedXml
object
signedXml.KeyInfo = getKeyInfo();
// Compute the signature
signedXml.ComputeSignature();
// Get the XML representation of the signature and save it to an
XmlElement object
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document
Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature,
true));
Many thanks,
AK