Signed and Verify xml file in C# -


i signing xml file using sign , verify xml

but when signing in 1 machine , try verify in other machine .

its failed . how can verify signed xml file in other machine .

this code

 private void button1_click(object sender, eventargs e)     {         try         {             // create new cspparameters object specify              // key container.             cspparameters cspparams = new cspparameters();             cspparams.keycontainername = "xml_dsig_rsa_key";              // create new rsa signing key , save in container.              rsacryptoserviceprovider rsakey = new rsacryptoserviceprovider(cspparams);              // create new xml document.             xmldocument xmldoc = new xmldocument();              // load xml file xmldocument object.             xmldoc.preservewhitespace = true;             xmldoc.load(textbox1.text);              // sign xml document.              signxml(xmldoc, rsakey);              console.writeline("xml file signed.");              // save document.             xmldoc.save(textbox1.text);            }         catch (exception ex)         {         //    console.writeline(e.message);         }     }       public static void signxml(xmldocument xmldoc, rsa key) {     // check arguments.      if (xmldoc == null)         throw new argumentexception("xmldoc");     if (key == null)         throw new argumentexception("key");      // create signedxml object.     signedxml signedxml = new signedxml(xmldoc);      // add key signedxml document.     signedxml.signingkey = key;      // create reference signed.     reference reference = new reference();     reference.uri = "";      // add enveloped transformation reference.     xmldsigenvelopedsignaturetransform env = new xmldsigenvelopedsignaturetransform();     reference.addtransform(env);      // add reference signedxml object.     signedxml.addreference(reference);      // compute signature.     signedxml.computesignature();      // xml representation of signature , save      // xmlelement object.     xmlelement xmldigitalsignature = signedxml.getxml();      // append element xml document.     xmldoc.documentelement.appendchild(xmldoc.importnode(xmldigitalsignature, true));  }       private void button2_click(object sender, eventargs e)      {          try          {              // create new cspparameters object specify               // key container.              cspparameters cspparams = new cspparameters();              cspparams.keycontainername = "xml_dsig_rsa_key";               // create new rsa signing key , save in container.               rsacryptoserviceprovider rsakey = new rsacryptoserviceprovider(cspparams);               // create new xml document.              xmldocument xmldoc = new xmldocument();               // load xml file xmldocument object.              xmldoc.preservewhitespace = true;              xmldoc.load(textbox1.text);               // verify signature of signed xml.              console.writeline("verifying signature...");              bool result = verifyxml(xmldoc, rsakey);               // display results of signature verification                // console.               if (result)              {                  messagebox.show("verified");              }              else              {                  messagebox.show("not verified");              }           }          catch (exception ex)          {             // console.writeline(e.message);          }      }        // verify signature of xml file against asymmetric   // algorithm , return result.  public static boolean verifyxml(xmldocument doc, rsa key) {     // check arguments.      if (doc == null)         throw new argumentexception("doc");     if (key == null)         throw new argumentexception("key");      // create new signedxml object , pass      // xml document class.     signedxml signedxml = new signedxml(doc);      // find "signature" node , create new      // xmlnodelist object.     xmlnodelist nodelist = doc.getelementsbytagname("signature");      // throw exception if no signature found.      if (nodelist.count <= 0)     {         throw new cryptographicexception("verification failed: no signature found in document.");     }      // example supports 1 signature      // entire xml document.  throw exception       // if more 1 signature found.      if (nodelist.count >= 2)     {         throw new cryptographicexception("verification failed: more 1 signature found document.");     }      // load first <signature> node.       signedxml.loadxml((xmlelement)nodelist[0]);      // check signature , return result.      return signedxml.checksignature(key); } 

thanks


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -