Click here to Skip to main content
15,913,758 members
Home / Discussions / C#
   

C#

 
GeneralRe: text files in xml Pin
leppie23-Aug-04 3:54
leppie23-Aug-04 3:54 
GeneralRe: text files in xml Pin
David Salter23-Aug-04 4:04
David Salter23-Aug-04 4:04 
GeneralRe: text files in xml Pin
Heath Stewart23-Aug-04 5:59
protectorHeath Stewart23-Aug-04 5:59 
GeneralRe: text files in xml Pin
Salil Khedkar23-Aug-04 19:59
Salil Khedkar23-Aug-04 19:59 
GeneralRe: text files in xml Pin
Salil Khedkar23-Aug-04 20:09
Salil Khedkar23-Aug-04 20:09 
Generalsign a xml document with a x509 certificate Pin
Escroto23-Aug-04 2:11
Escroto23-Aug-04 2:11 
GeneralRe: sign a xml document with a x509 certificate Pin
Heath Stewart23-Aug-04 5:56
protectorHeath Stewart23-Aug-04 5:56 
GeneralRe: sign a xml document with a x509 certificate Pin
Escroto24-Aug-04 0:10
Escroto24-Aug-04 0:10 
I have read your article and i have used your method to sign and to check the signature.

When you sign you don´t add the certificate info, so i add it in order to get it in the server when he receives it.

To perform the signature, it works, but when i verify it...the checksignature method always returns false...i have tried millions of things and none works.

Here i show you some code to sign:
<br />
//CODE TO SIGN THE DOCUMENT<br />
<br />
Microsoft.Web.Services.Security.X509.X509Certificat cert;<br />
		<br />
XmlDocument doc = new XmlDocument();<br />
doc.Load("c:/Formtosign.xml");<br />
		<br />
// Creating the XML signing object.<br />
SignedXml sxml = new SignedXml(doc);<br />
sxml.SigningKey = cert.key; <br />
<br />
// Set the canonicalization method for the document.<br />
sxml.SignedInfo.CanonicalizationMethod =<br />
              SignedXml.XmlDsigCanonicalizationUrl; // No comments.<br />
<br />
// Create an empty reference (not enveloped) for the XPath<br />
// transformation.<br />
Reference r = new Reference("");<br />
<br />
// Create the XPath transform and add it to the reference list.<br />
r.AddTransform(new XmlDsigEnvelopedSignatureTransform(false));<br />
<br />
// Add the reference to the SignedXml object.<br />
sxml.AddReference(r);<br />
<br />
///INSERT THE CERTIFICATE INTO THE SIGNED XML<br />
System.Security.Cryptography.Xml.KeyInfo keyInfo = <br />
               new   System.Security.Cryptography.Xml.KeyInfo(); <br />
	<br />
keyInfo.AddClause(new System.Security.Cryptography.Xml.KeyInfoX509Data(cert)); <br />
sxml.KeyInfo = keyInfo;		<br />
<br />
// Compute the signature.<br />
sxml.ComputeSignature();<br />
<br />
// Get the signature XML and add it to the document element.<br />
XmlElement sig = sxml.GetXml();<br />
doc.DocumentElement.AppendChild(sig);<br />
<br />
// Write-out formatted signed XML to console (allow for redirection).<br />
XmlTextWriter writer = new XmlTextWriter<br />
                 ("c:/Formsigned.xml",System.Text.Encoding.UTF8);<br />
writer.Formatting = Formatting.Indented;<br />
<br />
try<br />
{<br />
doc.WriteTo(writer);<br />
}<br />
finally<br />
{<br />
writer.Flush();<br />
writer.Close();<br />
}<br />


Now the code to verify the signature;
<br />
//CODE TO VERIFY THE SIGNATURE<br />
nodeList = doc.GetElementsByTagName<br />
             ("Signature",SignedXml.XmlDsigNamespaceUrl);<br />
<br />
//GETING THE CERTIFICATE<br />
nodeList2 = doc.GetElementsByTagName("X509Certificate");<br />
				                     <br />
SignedXml[] signatures = new SignedXml[nodeList.Count];<br />
			<br />
validSignatures = nodeList.Count;<br />
<br />
					<br />
for(int i = 0; i < nodeList.Count; i++)<br />
{<br />
	signatures[i] = new SignedXml();<br />
	signatures[i].LoadXml((XmlElement)nodeList[i]);<br />
<br />
<br />
	//I USE CAPICOM BECAUSE LATER I WILL NEED IT TO... <br />
        //...MAKE SOME VERIFICATIONS<br />
        Certificate cerCapi = new CertificateClass();<br />
        cerCapi.Import(nodeList2[i].InnerText);<br />
	ICertContext iCertCntxt = (ICertContext) cerCapi;<br />
	int certcntxt = iCertCntxt.CertContext ;<br />
	IntPtr hCertCntxt = new IntPtr(certcntxt);<br />
<br />
Microsoft.Web.Services.Security.X509.X509Certificate cer = new Microsoft.Web.Services.Security.X509.X509Certificate(hCerCntx);<br />
							<br />
	<br />
	RSACryptoServiceProvider csp = new RSACryptoServiceProvider();<br />
	csp = (RSACryptoServiceProvider)cer.PublicKey;<br />
	if (!signatures[i].CheckSignature(csp))<br />
		validSignatures--;<br />
}<br />


Your method is adapted to my necesities...but i think that is nearly the same. I get the keys not from a program, I get them from the certificate.

Thank you in advance...
GeneralFacing problem while calling FOR EACH st. in vb from C# DLL Pin
Ami Shah23-Aug-04 2:06
Ami Shah23-Aug-04 2:06 
GeneralRe: Facing problem while calling FOR EACH st. in vb from C# DLL Pin
Colin Angus Mackay23-Aug-04 2:24
Colin Angus Mackay23-Aug-04 2:24 
GeneralRe: Facing problem while calling FOR EACH st. in vb from C# DLL Pin
Ami Shah23-Aug-04 2:37
Ami Shah23-Aug-04 2:37 
GeneralRe: Facing problem while calling FOR EACH st. in vb from C# DLL Pin
Colin Angus Mackay23-Aug-04 3:19
Colin Angus Mackay23-Aug-04 3:19 
GeneralRe: Facing problem while calling FOR EACH st. in vb from C# DLL Pin
Ami Shah23-Aug-04 3:34
Ami Shah23-Aug-04 3:34 
GeneralRe: Facing problem while calling FOR EACH st. in vb from C# DLL Pin
Colin Angus Mackay23-Aug-04 4:03
Colin Angus Mackay23-Aug-04 4:03 
GeneralRe: Facing problem while calling FOR EACH st. in vb from C# DLL Pin
leppie23-Aug-04 4:02
leppie23-Aug-04 4:02 
GeneralDesigner - Components - Properties only visible in derived classes Pin
STW23-Aug-04 1:45
STW23-Aug-04 1:45 
GeneralRe: Designer - Components - Properties only visible in derived classes Pin
leppie23-Aug-04 4:00
leppie23-Aug-04 4:00 
GeneralRe: Designer - Components - Properties only visible in derived classes Pin
STW23-Aug-04 6:26
STW23-Aug-04 6:26 
QuestionC# Delete and Write specific data in a text file??? Pin
gman4423-Aug-04 1:07
gman4423-Aug-04 1:07 
AnswerRe: C# Delete and Write specific data in a text file??? Pin
leppie23-Aug-04 3:57
leppie23-Aug-04 3:57 
Generalhowto add control dynamicaly Pin
Stephan Wright22-Aug-04 23:56
Stephan Wright22-Aug-04 23:56 
GeneralRe: howto add control dynamicaly Pin
S Sansanwal23-Aug-04 0:20
S Sansanwal23-Aug-04 0:20 
GeneralRe: howto add control dynamicaly Pin
Stephan Wright23-Aug-04 0:39
Stephan Wright23-Aug-04 0:39 
GeneralRe: howto add control dynamicaly Pin
Nick Parker23-Aug-04 4:07
protectorNick Parker23-Aug-04 4:07 
GeneralRe: howto add control dynamicaly Pin
LongRange.Shooter23-Aug-04 7:37
LongRange.Shooter23-Aug-04 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.