I need to send a digital signature as one the parameters to an external webservice.
The steps to create as per documentation is :
- Create a
DOM
representation of theXML
data - Create a canonicalised representation of the
DOM
data. The canonicalised representation should follow the form described in http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments; - Create the signature
RSA
encryption of theSHA1
digest of the canonicalised representation. The signature is encrypted using the Participant‟s private key; - Encode the binary signature into a base64-encoded string
- Place the Signature string in the
SOAP
messageReqDigSig
element; - Store the XML data as it may be needed later to support Non-Repudiation of the submitted XML data.
I have used the following code:
private string SignXML4(X509Certificate2 Cert, string data)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = false;
xmlDoc.LoadXml(data);
XmlDsigC14NWithCommentsTransform t = new XmlDsigC14NWithCommentsTransform();
t.LoadInput(xmlDoc);
Stream s = (Stream)t.GetOutput(typeof(Stream));
SHA1 sha1 = SHA1.Create();
byte[] hash = sha1.ComputeHash(s);
RSACryptoServiceProvider rsaKey =
(RSACryptoServiceProvider)Cert.PrivateKey;
RSAParameters rsaPrivateParams = rsaKey.ExportParameters(true);
rsaKey.ImportParameters(rsaPrivateParams);
byte[] signature = rsaKey.Encrypt(hash, false);
return Convert.ToBase64String(signature);
}
But the response from the webservice says digital signature verification error.
is the code above as per the description in the documentation? How would i verify if the digital signaature is valid? is there any online tool?
Aucun commentaire:
Enregistrer un commentaire