Hi Guys I am writing the c# code for signing the xml document .
what i am trying to do here is that we have php code which is doing the siging of XML document and we are sending xml documet to the one of external service to get the data from there .
but now we decided to write the c# code for that .I wrote the code but problem is when i am sending the Signed XML document to the service it says invalid certficate .
1)my question is that where the c#.net SignedXml class is providing the same functionality like xmlseclibs.php in php
2)Signing Algoritms are the same or Diffrent.
Both the Xml File having the same xml structure but only the SignatureValue, X509Certificate, Modulus, DigestValue, Exponent values are different that i dont know why .
Can you guys please Check my code and tell me out what is problem and give me guide line how to fixed this problem .
Thanks In advanced .
This is my c# code :
//path of the PFX file
string pfxFilePath = @"c:\a.p12";
X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(privatekeypath), "PrivateKey");
doc.PreserveWhitespace = false;
SignedXml signedXml = new SignedXml(doc);
signedXml.SigningKey = cert.PrivateKey;
Reference reference = new Reference();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
reference.AddTransform(new XmlDsigExcC14NTransform());
signedXml.AddReference(reference);
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new KeyInfoX509Data(cert));
keyInfo.AddClause(new RSAKeyValue((RSA)cert.PrivateKey));
signedXml.KeyInfo = keyInfo;
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
var securityNode = doc.GetElementsByTagName("Security").Item(0);
securityNode.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
And Following is my Signed XML file by C# Code :
<?xml version="1.0" encoding="utf-8"?>
<ABCService version="2.0">
<Security>
<SignedInfo>
<Reference URI="">
<Transforms>
</Transforms>
<DigestValue>DigestValue</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>SignatureValue</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>X509Certificate</X509Certificate>
</X509Data>
<KeyValue>
<RSAKeyValue>
<Modulus>Modulus</Modulus>
<Exponent>Exponent</Exponent>
</RSAKeyValue>
</KeyValue>
</KeyInfo>
</Signature>
</Security>
<Service timestamp="2015-08-19 18:19:54.474">
<Online>
<Request />
</Online>
</Service>
<Data language="DEU">
some data
</Data>
</ABCService>
This is PHP code for siging the XML file :
/* Load the private key. */
$objKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA1, array('type' => 'private'));
/* Passphrase */
if ($passphrase)
{
$objKey->setPassphrase($passphrase);
}
$objKey->loadKey($privKey, TRUE);
/* Sign the metadata with our private key. */
$objXMLSecDSig = new XMLSecurityDSig();
$objXMLSecDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N);
$objXMLSecDSig->addReference(
$rootNode,
XMLSecurityDSig::SHA1,
);
$objXMLSecDSig->sign($objKey);
/* Add the certificate to the signature. */
$objXMLSecDSig->add509Cert($pubKey, TRUE, TRUE, TRUE);
/* Add the signature. */
$objXMLSecDSig->insertSignature($xml->getElementsByTagName("Security")->item(0));
/* Return the DOM tree as a string. */
file_put_contents("c3.xml", $xml->saveXML());
return $xml->saveXML();
this is signed file by using the xmlseclibs.php Using the php code :
<?xml version="1.0" encoding="utf-8"?>
<ABCService version="2.0">
<DigestValue>DigestValue</DigestValue></Reference></SignedInfo>
<SignatureValue>SignatureValue</SignatureValue>
<KeyInfo><X509Data>
<X509Certificate>X509Certificate</X509Certificate></X509Data><KeyValue><RSAKeyValue>
<Modulus>Modulus</Modulus><Exponent>Exponent</Exponent></RSAKeyValue></KeyValue></KeyInfo></Signature></Security>
<Service timestamp="2015-08-19 17:10:10.324">
<Online>
<Request/>
</Online>
</Service>
<Data language="DEU">
Data
</Data>
</ABCService >
: