473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 10171
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kim H Madsen | last post by:
I have created a .Net Service that is sending mails using SMTP Server/Exchange Server how do i put in a Digital Signature so the reciver is 100% sure that the mail i from the owner of Server where...
1
by: Arkady Frenkel | last post by:
Hi! I try to make digital signature of the blob and check it , but on check ( signature created and seems OK ) I catch exception : "An XmlDocument context is required to resolve the Reference Uri...
0
by: CLarkou | last post by:
I bought a digital signature for my MSaccess application in Office 2003. I select TOOLS\Digital Signatures in Visual Basic Editor and I am not able to see my digital signature in the available...
0
by: - | last post by:
I use a code very similar to that found in the MSDN sample attached to the class XmlDsigEnvelopedSignatureTransform. The code works fine and produces somethink like <Signature...
1
by: Marco Moioli | last post by:
Hi, I have a problem with a X509 digital signature. I want to sign a Word 2003 document saved in .xml format. the problem is that after the signature, Word 2003 don't want to open the document. ...
0
by: Geagleeye | last post by:
Hi everyone. I have some vba code to generate a pdf document through word, and add also digital signature. My problem is : how can i change the way the signature layout, it always show the...
2
by: scottpet | last post by:
Hi, I want to add a namespace prefix to the root node of an object I am serializing to XML. I have been reading though this article:...
3
by: itcoll | last post by:
i have wriiten java code for client server communication - the client sends a digital signature and the server verifies it using the public key .I have sent the signature as a string from the client...
3
by: tmoloy | last post by:
I am using RFID tags to store some data which will then be read by a 3rd party. I need to include a digital signature (or some variation) along with the data so that the 3rd party can verify the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.