Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to generate Signature of a Xml content with current date using RSA Algorithm. I am generating the Signature for Xml with date while generating signature but getting signature mismatch . Updated my code as below.

What I have tried:

My code is as below:
<pre> byte[] contentbytes = System.Text.Encoding.UTF8.GetBytes(content + CurrentDate);


           

            string contentdatatag = "<![CDATA[" + content + "]]>";


            //string decodedxml = HttpUtility.HtmlDecode(contentdatatag);

            //string decodedxml = contentdatatag.Replace("<", "<").Replace(">", ">");

            XmlNodeList elemList = xmlDoc2.GetElementsByTagName("content");

            elemList[0].InnerText = contentdatatag;





            XmlNodeList elemList1 = xmlDoc2.GetElementsByTagName("date");

            elemList1[0].InnerText = CurrentDate;




            //content = content + elemList1[0].InnerText;

            //string content = System.IO.File.ReadAllText(ToSignFilePath);
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

           

            Stream mStream1 = new MemoryStream(contentbytes);


            string sign = Getsignature(pfxFilePath, "1", mStream1, xmlDoc2);





// bool isValid = privateKey1.VerifyData(data, "SHA256", signature);
return signedBlock4.ToString();
}

private string Getsignature(string path, string password, Stream Data,XmlDocument xmlDoc2)
    {
        string signedBlock4 = string.Empty;
        var collection = new X509Certificate2Collection();
        collection.Import(path, password, X509KeyStorageFlags.PersistKeySet);
        var certificate = collection[0];
        
        bool pvtKeyExists = certificate.HasPrivateKey;
        if (pvtKeyExists == true)
        {
            
            var privateKey = certificate.PrivateKey as RSACryptoServiceProvider;
         

            // Force use of the Enhanced RSA and AES Cryptographic Provider with openssl-generated SHA256 keys
            var enhCsp = new RSACryptoServiceProvider().CspKeyContainerInfo;
            var cspparams = new CspParameters(enhCsp.ProviderType, enhCsp.ProviderName, privateKey.CspKeyContainerInfo.KeyContainerName);
            privateKey = new RSACryptoServiceProvider(cspparams);

            //var signature = privateKey.SignData(Data, "SHA1");
            var signature = privateKey.SignData(Data, "SHA256");
            signedBlock4 = Convert.ToBase64String(signature);


            XmlNodeList elemList = xmlDoc2.GetElementsByTagName("signature");

            elemList[0].InnerText = signedBlock4;

           
        }
<pre>
Posted
Comments
Kornfeld Eliyahu Peter 24-May-17 6:40am    
Date is changing constantly?
[no name] 24-May-17 23:42pm    
Yes date is taken at the time of signature generation and we sign the required Xml content with the date when signature generate(DateTime.Now)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900