Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Proyecto SIF/Veri*Factu/Ley Antifraude > General/Noticias
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo Hace 1 Semana
rafa1963 rafa1963 is offline
Registrado
 
Registrado: dic 2024
Posts: 7
Poder: 0
rafa1963 Va por buen camino
Question Certificado o firma digital en archivo XML. AEAT

Buenas tardes compañeros, estoy intentando firmar con mi certificado digital con Delphi un archivo XML ya creado y no sé exactamente como crearlo.
Se que tengo que crear un dodo <signature>. La verdad es que no tengo mucha idea de como poder hacerlo.
Agradecería muchísimo vuestras opiniones y sugerencias.

Gracias.
Responder Con Cita
  #2  
Antiguo Hace 1 Semana
NANAUJ1970 NANAUJ1970 is offline
Miembro
NULL
 
Registrado: jul 2017
Posts: 9
Poder: 0
NANAUJ1970 Va por buen camino
Una buena forma de hacerlo es usando autofirma. Lo puedes descargar gratuitamente e instalarlo. Este accede al almacen de certificados de windows.

Aqui tienes un codigo en D7 para firmar un xml usando el metodo de autofirma. Hay que pasarle los parametros nombre del fichero xml normal y el nombre que quieres que cree firmado.

Código Delphi [-]
USES    Capicom_TLB,....

procedure FirmarXML(const XMLFileName: string; const SignedXMLFileName: string);
var
  CertStore: IStore;
  Certs: ICertificates;
  Cert: ICertificate2;
  i: Integer;
  CertName: string;
  CertBuscar: string;
  TempVar: OleVariant;
  TempCert: ICertificate;
  AliasCertificado: string;
  AutoFirmaPath: string;
  RutaAutoFirma: string;
  certpassword: string;
  Comando: string;
begin


  AutoFirmaPath:=ansilowercase(f.Buscarautofirmapath);
  AutoFirmaPath := trim(StringReplace(AutoFirmaPath, '"%1"', '', [rfReplaceAll]));
  AutoFirmaPath := trim(StringReplace(AutoFirmaPath, 'autofirma.exe', 'AutoFirmaCommandLine.exe', [rfReplaceAll]));
  if autoFirmaPath='' then  begin
      f.Aviso('error','No Se Encuentra AutoFirma En Registro '+autoFirmaPath);
      exit;
  end;
  rutaAutoFirma:=extractfilePath( autoFirmaPath )+'AutoFirmaCommandLine.exe';
  if not FileExists(rutaAutoFirma) then begin
      f.Aviso('error','No Se Encuentra Ejecutable AutoFirma '+rutaAutoFirma);
      exit;
  end;
  // AutoFirmaPath := 'C:\Program Files\AutoFirma\AutoFirma\AutoFirmaCommandLine.exe'; // path indicado manualmente

  try
    // Verificar que AutoFirma est� instalado
    if not FileExists(AutoFirmaPath) then
    begin
      ShowMessage('AutoFirma no encontrado en: ' + AutoFirmaPath);
      Exit;
    end;

    // Abrir el almac�n de certificados del usuario actual
    CertStore := CoStore.Create;
    CertStore.Open(CAPICOM_CURRENT_USER_STORE, 'My', CAPICOM_STORE_OPEN_READ_ONLY);
    Certs := CertStore.Certificates;

    // Buscar certificado por coincidencia en el SubjectName
    CertBuscar := 'nombreparcial'; // Cambiar por parte del nombre del certificado
    Cert := nil;
    for i := 1 to Certs.Count do
    begin
      TempVar := Certs.Item[i];
      if Supports(TempVar, ICertificate, TempCert) then
      begin
        if Pos(CertBuscar, TempCert.SubjectName) > 0 then
        begin
          Cert := TempCert as ICertificate2;
          AliasCertificado := TempCert.SubjectName;
          f_entrada.Memo1.Lines.Add('Certificado encontrado: ' + AliasCertificado);
          Break;
        end;
      end;
    end;

    if Cert = nil then
    begin
      ShowMessage('Certificado no encontrado con: ' + CertBuscar);
      Exit;
    end;

    AliasCertificado := ExtraerCN(TempCert.SubjectName);

    //AliasCertificado := 'ANTONIO_FLORES_41000000Z'; // SI QUIERES ESPECIFICAR EL NOMBRE DIRECTAMENTE

    // Construir el comando
    RutaAutoFirma := ExtractShortPathName(AutoFirmaPath);
    Comando := Format('%s sign -format xades -i "%s" -o "%s" -store windows -password "" -alias "%s"',
                    [RutaAutoFirma, XMLFileName, SignedXMLFileName, AliasCertificado]);

    // Ejecutar AutoFirma y esperar que termine
    ExecuteAndWait(Comando);


    // Verificar si se gener� el archivo firmado
    if not FileExists(SignedXMLFileName) then
      ShowMessage('Error firmando el XML.')

  except
    on E: Exception do begin
      ShowMessage('Excep: Error al firmar XML: ' + E.Message);
    end;
  end;

end;

procedure ExecuteAndWait(const aCommando: string);
var
  tmpStartupInfo: TStartupInfo;
  tmpProcessInformation: TProcessInformation;
  tmpProgram: String;
begin
  tmpProgram := trim(aCommando);
//  f_entrada.Memo1.Lines.add('Executing: ' + tmpProgram); // Mostrar la instrucción que se ejecuta
  FillChar(tmpStartupInfo, SizeOf(tmpStartupInfo), 0);
  with tmpStartupInfo do
  begin
    cb := SizeOf(TStartupInfo);
    wShowWindow := SW_HIDE;
  end;

  if CreateProcess(nil, pchar(tmpProgram), nil, nil, true, CREATE_NO_WINDOW,
    nil, nil, tmpStartupInfo, tmpProcessInformation) then
  begin
    // loop every 10 ms
    while WaitForSingleObject(tmpProcessInformation.hProcess, 10) > 0 do
    begin
      Application.ProcessMessages;
    end;
    CloseHandle(tmpProcessInformation.hProcess);
    CloseHandle(tmpProcessInformation.hThread);
  end
  else
  begin
    RaiseLastOSError;
  end;
end;

Última edición por dec fecha: Hace 6 Días a las 08:25:57. Razón: Poner etiquetas DELPHI
Responder Con Cita
  #3  
Antiguo Hace 1 Semana
NANAUJ1970 NANAUJ1970 is offline
Miembro
NULL
 
Registrado: jul 2017
Posts: 9
Poder: 0
NANAUJ1970 Va por buen camino
Como enviar xml

El problema que yo tengo es que me de errores al enviar el xml a preproduccion con cualquier codigo usando librerias de D7 con indy9,etc. Y prefiero no hacerlo con curl, ya que este necesita el certificado en pfx y su contraseña, y seria mas comodo hacerlo usando los certificacos del almacen de windows..

Alguien tiene un codigo en Delphi7 que funcione bien para enviar los xml usando un certificado personal del almacen de windows?

gracias de antemano.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
IdSSL y certificado digital el-otro Internet 1 03-01-2024 11:07:43
Problemas con certificado digital Paulao Varios 3 26-09-2011 17:00:46
Firma Digital Dado de baja Varios 5 30-11-2008 20:52:16
Firma Digital Tino JAVA 0 02-08-2007 01:01:31
firma digital felixgo Varios 2 16-02-2005 15:36:00


La franja horaria es GMT +2. Ahora son las 15:11:02.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi