469,646 Members | 1,163 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

How to add a namespace prefix to a digital signature

Hello,

I use a code very similar to that found in the MSDN sample attached to the
class XmlDsigEnvelopedSignatureTransform (code attached below).
The code works fine and produces somethink like
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod
Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
............
</Signature>

But I need the signature to be in a namespace that should be identified by
the ds prefix:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
.....
</ds:Signature>

How can I achive this?

Any help would be greatly appreciated
Code that I use:

// Sign an XML file and save the signature in a new file.
public static void SignXmlFile(string FileName, string SignedFileName,
RSA Key)
{
// Create a new XML document.
XmlDocument doc = new XmlDocument();

// Format the document to ignore white spaces.
doc.PreserveWhitespace = false;

// Load the passed XML file using it's name.
doc.Load(new XmlTextReader(FileName));

// Create a SignedXml object.
SignedXml signedXml = new SignedXml(doc);

// Add the key to the SignedXml document.
signedXml.SigningKey = Key;

// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";

// Add a transformation to the reference.
Transform trns = new XmlDsigC14NTransform();
reference.AddTransform(trns);

// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);

// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to
validate).
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((RSA)Key));
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();

// Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xml DigitalSignature,
true));
if (doc.FirstChild is XmlDeclaration)
{
doc.RemoveChild(doc.FirstChild);
}

// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new
UTF8Encoding(false));
doc.WriteTo(xmltw);
xmltw.Close();
}
Nov 12 '05 #1
2 9359
I solved it myself

Void ChangePrefix (XmlNode ^ pNode)
{
if ( !pNode)
return;

do
{
pNode->Prefix = "ds";

ChangePrefix (pNode->FirstChild);
}
while ( pNode = pNode->NextSibling );
}
"S. Baumann" <no*****@xyz.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

I use a code very similar to that found in the MSDN sample attached to the
class XmlDsigEnvelopedSignatureTransform (code attached below).
The code works fine and produces somethink like
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod
Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
...........
</Signature>

But I need the signature to be in a namespace that should be identified by
the ds prefix:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
....
</ds:Signature>

How can I achive this?

Any help would be greatly appreciated
Code that I use:

// Sign an XML file and save the signature in a new file.
public static void SignXmlFile(string FileName, string SignedFileName,
RSA Key)
{
// Create a new XML document.
XmlDocument doc = new XmlDocument();

// Format the document to ignore white spaces.
doc.PreserveWhitespace = false;

// Load the passed XML file using it's name.
doc.Load(new XmlTextReader(FileName));

// Create a SignedXml object.
SignedXml signedXml = new SignedXml(doc);

// Add the key to the SignedXml document.
signedXml.SigningKey = Key;

// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";

// Add a transformation to the reference.
Transform trns = new XmlDsigC14NTransform();
reference.AddTransform(trns);

// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);

// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key
to validate).
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((RSA)Key));
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();

// Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xml DigitalSignature,
true));
if (doc.FirstChild is XmlDeclaration)
{
doc.RemoveChild(doc.FirstChild);
}

// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new
UTF8Encoding(false));
doc.WriteTo(xmltw);
xmltw.Close();
}

Nov 12 '05 #2
Unfortunately this is not a solution. The signature can not be verified
afterwards.
I found a lot of questions similar to my question in the Internet. But
always
without a meaningful answer.

May be somebody from Microsoft can please answer this question. Even
an "currently impossible" would be helpfull. I already invested several days
into this topic.

"S. Baumann" <no*****@xyz.com> wrote in message
news:ea**************@TK2MSFTNGP09.phx.gbl...
I solved it myself

Void ChangePrefix (XmlNode ^ pNode)
{
if ( !pNode)
return;

do
{
pNode->Prefix = "ds";

ChangePrefix (pNode->FirstChild);
}
while ( pNode = pNode->NextSibling );
}
"S. Baumann" <no*****@xyz.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

I use a code very similar to that found in the MSDN sample attached to
the class XmlDsigEnvelopedSignatureTransform (code attached below).
The code works fine and produces somethink like
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod
Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
...........
</Signature>

But I need the signature to be in a namespace that should be identified
by the ds prefix:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
....
</ds:Signature>

How can I achive this?

Any help would be greatly appreciated
Code that I use:

// Sign an XML file and save the signature in a new file.
public static void SignXmlFile(string FileName, string SignedFileName,
RSA Key)
{
// Create a new XML document.
XmlDocument doc = new XmlDocument();

// Format the document to ignore white spaces.
doc.PreserveWhitespace = false;

// Load the passed XML file using it's name.
doc.Load(new XmlTextReader(FileName));

// Create a SignedXml object.
SignedXml signedXml = new SignedXml(doc);

// Add the key to the SignedXml document.
signedXml.SigningKey = Key;

// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";

// Add a transformation to the reference.
Transform trns = new XmlDsigC14NTransform();
reference.AddTransform(trns);

// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);

// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key
to validate).
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((RSA)Key));
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();

// Append the element to the XML document.

doc.DocumentElement.AppendChild(doc.ImportNode(xml DigitalSignature,
true));
if (doc.FirstChild is XmlDeclaration)
{
doc.RemoveChild(doc.FirstChild);
}

// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new
UTF8Encoding(false));
doc.WriteTo(xmltw);
xmltw.Close();
}


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kim H Madsen | last post: by
1 post views Thread by Arkady Frenkel | last post: by
reply views Thread by CLarkou | last post: by
1 post views Thread by Marco Moioli | last post: by
reply views Thread by Geagleeye | last post: by
2 posts views Thread by scottpet | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.