Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
 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();
     }
 }