Generate XML Digital Signature in C#

GreenEyes

New Member
I am trying to sign some XML data using PIV card. In order to use PKCS#1 v1.5 pading, etc. I cannot use the built-in .Net XML Signature generation APIs. I was wondering how do I accomplish setp #3 below. Any help would be highly appreciated.\[code\] private static string GenerateXmlSignature( XmlDocument xmlDocument, X509Certificate2 certificate) { // Step # 1: Canonicalize, transform and generate digest byte[] dataToHash = Encoding.UTF8.GetBytes(xmlDocument.OuterXml); XmlDsigC14NTransform transformData = http://stackoverflow.com/questions/14533245/new XmlDsigC14NTransform(false); transformData.LoadInput(new MemoryStream(dataToHash)); byte[] bDigestData = transformData.GetDigestedOutput(SHA256Managed.Create()); string sDigestData = Convert.ToBase64String(bDigestData); Console.WriteLine("DigestValue: " + sDigestData); // Step # 2: Construct SignedInfo block string sSignedInfoXml = string.Format( "<SignedInfo>" + "<CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/xml-c14n\"/>" + "<SignatureMethod Algorithm=\"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256\"/>" + "<Reference URI=\"#signedData\">" + "<Transforms><Transform Algorithm=\"http://www.w3.org/TR/xml-c14n\"/></Transforms>" + "<DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha256\"/>" + "<DigestValue>{0}</DigestValue>" + "</Reference>" + "</SignedInfo>", sDigestData); byte[] bSignedInfoXml = Encoding.UTF8.GetBytes(sSignedInfoXml); Console.WriteLine("SignedInfo: " + sSignedInfoXml); // Step # 3: Encrypt with PKCS#1 v1.5 using private key // How do I perform the PKCS#1 v1.5 encryption using the provate key? // ????? // ????? // Step # 4: Cosntruct the Signature block string signatureValue = http://stackoverflow.com/questions/14533245/Convert.ToBase64String(signedHash); string xmlSignature ="<Signature Id=\"Test\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\">" + sSignedInfoXml + "<SignatureValue>" + signatureValue + "</SignatureValue>" + "<KeyInfo><X509Data><X509Certificate>" + Convert.ToBase64String(certificate.PublicKey.EncodedKeyValue.RawData) + "</X509Certificate></X509Data></KeyInfo>" + "</Signature>"; Console.WriteLine(xmlSignature); }\[/code\]Thanks a lot in advance!
 
Back
Top