public static void Firmar(string origen, string destino, string rutaFirma, string contraseniaFirma) { PrivateKey privateKey; Provider provider; X509Certificate certificate = LoadCertificate(rutaFirma, contraseniaFirma, out privateKey, out provider); //Si encontramos el certificado... if (certificate != null) { //Política de firma (Con las librerías JAVA, esto se define en tiempo de ejecución) TrustFactory.instance = TrustExtendFactory.newInstance(); TrustFactory.truster = MyPropsTruster.getInstance(); PoliciesManager.POLICY_SIGN = new Facturae31Manager(); PoliciesManager.POLICY_VALIDATION = new Facturae31Manager(); TrustFactory.instance = TrustFactory.newInstance(); TrustFactory.truster = PropsTruster.getInstance(); PoliciesManager.POLICY_SIGN = new Facturae31Manager(); PoliciesManager.POLICY_VALIDATION = new Facturae31Manager(); DataToSign dataToSign = new DataToSign(); dataToSign.setXadesFormat(EnumFormatoFirma.XAdES_BES); dataToSign.setEsquema(XAdESSchemas.XAdES_132); dataToSign.setXMLEncoding("UTF-8"); dataToSign.setEnveloped(true); dataToSign.setParentSignNode("comprobante"); dataToSign.addObject(new ObjectToSign(new InternObjectToSign("comprobante"), "contenido comprobante", null, "text/xml", null)); dataToSign.setDocument(Erp90w(origen)); object[] objArray = (new FirmaXML()).signFile(certificate, dataToSign, privateKey, provider); FileOutputStream fileOutputStream = new FileOutputStream(destino); UtilidadTratarNodo.saveDocumentToOutputStream((Document)objArray[0], fileOutputStream, true); fileOutputStream.close(); } }
public Boolean Firmar(string RutaCertificado, string Clave, string RutaXML, string RutaFirmado, ref string mensaje) { try { X509Certificate certificate = default(X509Certificate); PrivateKey key = default(PrivateKey); Provider provider = default(Provider); string str = Clave; KeyStore store = KeyStore.getInstance("PKCS12"); store.load(new FileInputStream(RutaCertificado), str.ToCharArray()); Enumeration enumeration = store.aliases(); while (enumeration.hasMoreElements()) { string alias1 = Convert.ToString(enumeration.nextElement()); if (store.isKeyEntry(alias1)) { //certificate = (X509Certificate)store.getCertificate(alias1); certificate = (X509Certificate)store.getCertificate(alias1); key = (PrivateKey)store.getKey(alias1, str.ToCharArray()); provider = store.getProvider(); break; } } PrivateKey key2 = key; Provider provider2 = provider; if (certificate != null) { TrustFactory.instance = TrustFactory.newInstance(); TrustFactory.truster = PropsTruster.getInstance(); DataToSign dataToSign = new DataToSign(); dataToSign.setXadesFormat(EnumFormatoFirma.XAdES_BES); dataToSign.setEsquema(XAdESSchemas.XAdES_132); dataToSign.setPolicyKey("facturae31"); dataToSign.setXMLEncoding("UTF-8"); dataToSign.setEnveloped(true); dataToSign.addObject(new ObjectToSign(new InternObjectToSign("comprobante"), "contenido comprobante", null, "text/xml", null)); dataToSign.setParentSignNode("comprobante"); dataToSign.setDocument(LoadXML(RutaXML)); object[] objArray = new FirmaXML().signFile(certificate, dataToSign, key, provider); FileOutputStream outputStream = new FileOutputStream(RutaFirmado); UtilidadTratarNodo.saveDocumentToOutputStream((Document)objArray[0], outputStream, true); outputStream.flush(); outputStream.close(); } return(true); } catch (Exception exception1) { //ProjectData.SetProjectError(exception1); //Exception exception = exception1; mensaje = "Error al Firmar el Documento : " + exception1.Message; //ProjectData.ClearProjectError(); return(false); //ProjectData.ClearProjectError(); } }
public static void firmar(string Archivo) { string path = @"C:\Firma\fabricio_fortunato_mero_mosquera.p12"; PrivateKey privatekey; Provider provider; java.security.cert.X509Certificate certificate = LayerLogic.ClassLibrary.Complementos.Firmar.loadCertificate(path, "FFmm_1978", out privatekey, out provider); if (certificate != null) { //Creamos el documento a firmar DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); //C# var base64 = System.Convert.FromBase64String(Archivo); string bytes = System.Text.Encoding.UTF8.GetString(base64); ByteArrayInputStream bs = new ByteArrayInputStream(System.Text.Encoding.UTF8.GetBytes(bytes)); Document documento = dbf.newDocumentBuilder().parse(bs); //Creamos datos a firmar DataToSign dataToSign = new DataToSign(); dataToSign.setXadesFormat(EnumFormatoFirma.XAdES_BES); //XAdES-EPES dataToSign.setAddPolicy(false); dataToSign.setXMLEncoding(encoding); dataToSign.setEnveloped(true); dataToSign.addObject(new ObjectToSign(new InternObjectToSign(nodoFirma), "comprobante", null, "text/xml", null)); dataToSign.setParentSignNode(nodoFirma); //dataToSign.setDocument(LoadXML(NombreArchivo)); dataToSign.setDocument(documento); //Firmar Object[] res = new FirmaXML().signFile(certificate, dataToSign, privatekey, provider); Document doc = (Document)res[0]; //Transformar a string org.w3c.dom.ls.DOMImplementationLS domImplementation = (org.w3c.dom.ls.DOMImplementationLS)doc.getImplementation(); org.w3c.dom.ls.LSSerializer lsSerializer = domImplementation.createLSSerializer(); Archivo = lsSerializer.writeToString(doc).Replace("UTF-16", "UTF-8"); //C# var ArchivoFirmado = Encoding.UTF8.GetBytes(Archivo); string firmado = Convert.ToBase64String(ArchivoFirmado); var base642 = System.Convert.FromBase64String(firmado); string bytes2 = System.Text.Encoding.UTF8.GetString(base642); } }