wilcg
21-10-2018, 02:53:27
Amigos del foro necesito su ayuda,
esta funcion sirve para firmar un XML con certificado digital, pero esta en lenguaje C#, no soy bueno en este lenguaje quisiera su ayuda para convertirlo a Delphi. Espero su ayuda es muy importante para mi proyecto.
public static void firmar(string path_cert, string pass_cert, string path_xml,string path_guardar)
{
System.Security.Cryptography.X509Certificates.X509Certificate2 certificado
= new System.Security.Cryptography.X509Certificates.X509Certificate2(path_cert,pass_cert,
System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable);
var xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(path_xml);
var nodoExtension =
xmlDoc.GetElementsByTagName(
"ExtensionContent",
"urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2").Item(0);
var signedXml = new SignedXml(xmlDoc) { SigningKey = certificado.PrivateKey };
var xmlSignature = signedXml.Signature;
var env = new XmlDsigEnvelopedSignatureTransform();
var reference = new Reference(string.Empty);
reference.AddTransform(env);
xmlSignature.SignedInfo.AddReference(reference);
var keyInfo = new KeyInfo();
var x509Data = new KeyInfoX509Data(certificado);
x509Data.AddSubjectName(certificado.Subject);
keyInfo.AddClause(x509Data);
xmlSignature.KeyInfo = keyInfo;
xmlSignature.Id = "Sunat";
signedXml.ComputeSignature();
nodoExtension.AppendChild(signedXml.GetXml());
using (var memDoc = new System.IO.MemoryStream())
{
using (var writer = XmlWriter.Create(memDoc,
new XmlWriterSettings { Encoding = Encoding.GetEncoding("UTF-8") }))
{
xmlDoc.WriteTo(writer);
}
}
xmlDoc.Save(path_guardar);
}
esta funcion sirve para firmar un XML con certificado digital, pero esta en lenguaje C#, no soy bueno en este lenguaje quisiera su ayuda para convertirlo a Delphi. Espero su ayuda es muy importante para mi proyecto.
public static void firmar(string path_cert, string pass_cert, string path_xml,string path_guardar)
{
System.Security.Cryptography.X509Certificates.X509Certificate2 certificado
= new System.Security.Cryptography.X509Certificates.X509Certificate2(path_cert,pass_cert,
System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable);
var xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(path_xml);
var nodoExtension =
xmlDoc.GetElementsByTagName(
"ExtensionContent",
"urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2").Item(0);
var signedXml = new SignedXml(xmlDoc) { SigningKey = certificado.PrivateKey };
var xmlSignature = signedXml.Signature;
var env = new XmlDsigEnvelopedSignatureTransform();
var reference = new Reference(string.Empty);
reference.AddTransform(env);
xmlSignature.SignedInfo.AddReference(reference);
var keyInfo = new KeyInfo();
var x509Data = new KeyInfoX509Data(certificado);
x509Data.AddSubjectName(certificado.Subject);
keyInfo.AddClause(x509Data);
xmlSignature.KeyInfo = keyInfo;
xmlSignature.Id = "Sunat";
signedXml.ComputeSignature();
nodoExtension.AppendChild(signedXml.GetXml());
using (var memDoc = new System.IO.MemoryStream())
{
using (var writer = XmlWriter.Create(memDoc,
new XmlWriterSettings { Encoding = Encoding.GetEncoding("UTF-8") }))
{
xmlDoc.WriteTo(writer);
}
}
xmlDoc.Save(path_guardar);
}