Hi Team,
How to store the public and private key in xml file and extract the same key to create signature/ verify signature.
CODE:
public string SignXMLD(string rawxml) ///Signature Creation Method
{
//Declaring variables
string SourceData;
byte[] tmpSource;
byte[] tmpHash;
SourceData = "AgencyBanking";
////Declaring variables
//create a byte array from source data
tmpSource = ASCIIEncoding.ASCII.GetBytes(SourceData);
Console.WriteLine("Key Pairs are generating .............please wait for few moments.......");
//RSAKeyPairGenerator generates the RSA key pair based on the random number and strength of the key required
RsaKeyPairGenerator rsaKeyPairGen = new RsaKeyPairGenerator();
rsaKeyPairGen.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
keyPair = rsaKeyPairGen.GenerateKeyPair();
//Extracting the Private key from pair
RsaKeyParameters PrivateKey = (RsaKeyParameters)keyPair.Private;
//Store the private key in xml file
//How to store the private key generated from keypair into xml file and extract
//the same key and use it to generate the signature
string str = PrivateKey.FromXmlString(true);
//Generate the Digital Signature
ISigner sign = SignerUtilities.GetSigner(PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id);
sign.Init(true, PrivateKey);
sign.BlockUpdate(tmpSource, 0, tmpSource.Length);
byte[] signature = sign.GenerateSignature();
string xyz = Convert.ToBase64String(signature);
Console.WriteLine();
Console.WriteLine("The Digital Signature is : ");
Console.WriteLine(ByteArrayToString(signature));
XmlDocument objdocument = new XmlDocument();
objdocument.LoadXml(rawxml);
XmlDocument newXmlr = new XmlDocument();
newXmlr.LoadXml("<root></root>");
XmlNode rootNoder = newXmlr.ImportNode(objdocument.DocumentElement, true);
newXmlr.DocumentElement.AppendChild(rootNoder);
XmlDocument newXmlnew = new XmlDocument();
XmlNode root = newXmlr.DocumentElement;
//Create a new node.
XmlElement elem = newXmlr.CreateElement("Signature");
elem.InnerText = xyz;
root.AppendChild(elem);
string strXML = root.OuterXml;
return strXML;
}
static string ByteArrayToString(byte[] arrInput)
{
int i;
StringBuilder sOutput = new StringBuilder(arrInput.Length);
for (i = 0; i < arrInput.Length; i++)
{
sOutput.Append(arrInput[i].ToString("X".ToLower()));
}
return sOutput.ToString();
}
public bool SignXMLDResponse(string rawxml) //// SIgnature Verification Method
{
//Declaring variables
string SourceData;
byte[] tmpSource;
byte[] tmpHash;
SourceData = "AgencyBanking";
//create a byte array from source data
tmpSource = ASCIIEncoding.ASCII.GetBytes(SourceData);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Key Pairs are generating .............please wait for few moments.......");
//Extracting the Private key from pair
bool status = false;
RsaKeyParameters PublicKey = (RsaKeyParameters)keyPair.Public;
//Verification of the Digital Signature
try
{
XmlDocument newXmlnew = new XmlDocument();
newXmlnew.LoadXml(rawxml);
XmlNodeList nodeList = newXmlnew.GetElementsByTagName("Signature");
foreach (XmlNode node in nodeList)
{
string s = node.InnerXml;
byte[] signaturenew = Convert.FromBase64String(s);
ISigner sign1 = SignerUtilities.GetSigner(PkcsObjectIdentifiers.Sha1WithRsaEncryption.Id);
sign1.Init(false, PublicKey);
sign1.BlockUpdate(tmpSource, 0, tmpSource.Length);
status = sign1.VerifySignature(signaturenew);
}
}
catch (Exception e)
{
Console.WriteLine("The Digital Signature is Valid" + e.Message + e.StackTrace);
}
return status;
}
Please assist me regarding this
Thanks