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 + "]]>";
XmlNodeList elemList = xmlDoc2.GetElementsByTagName("content");
elemList[0].InnerText = contentdatatag;
XmlNodeList elemList1 = xmlDoc2.GetElementsByTagName("date");
elemList1[0].InnerText = CurrentDate;
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;
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, "SHA256");
signedBlock4 = Convert.ToBase64String(signature);
XmlNodeList elemList = xmlDoc2.GetElementsByTagName("signature");
elemList[0].InnerText = signedBlock4;
}
<pre>