Ver Mensaje Individual
  #5  
Antiguo 24-09-2014
shoulder shoulder is offline
Miembro
 
Registrado: abr 2008
Posts: 441
Reputación: 17
shoulder Va por buen camino
Capicom

Gracias habia probado con esa pagina integre las funciones y el procedimiento pero me da siempre error en HYMSignature, agregue la Unit Types para solucionar el error de TByteDynArray
Código Delphi [-]
 
function ExtractSignedContent(
  const AFileName: string): string;
var
  fs: TFileStream;
  iHeadLen, iContentLen, iPos: Integer;
  sContentLength: string;
  ms: TMemoryStream;
  BDA_Head, BDA_Cont: TByteDynArray;
begin
  Result := '';
  if not FileExists(AFileName) then
    raise Exception.Create('??"' + AFileName + '"???');
  iHeadLen := Length(HYMSignature) + HashString_Length;
  SetLength(BDA_Head, iHeadLen);
  ms:= TMemoryStream.Create;
  ms.LoadFromFile(AFileName);
  fs := TFileStream.Create(AFileName, fmCreate);
  try
    ms.Position:= 0;  
    ms.Read(BDA_Head[0], iHeadLen);  
    sContentLength := Byte2String(BDA_Head); //??????
    iPos := Pos(HYMSignature, sContentLength);  
    if iPos > 0 then  
    begin  
      //????
      iContentLen := StrToInt(Copy(sContentLength, Length(HYMSignature) + 1, MaxInt));
      SetLength(BDA_Cont, iContentLen);  
      ms.Read(BDA_Cont[0], iContentLen);  
      Result := Byte2String(BDA_Cont);  
      //??????????????
      fs.CopyFrom(ms, ms.Size - ms.Position); //?????????????
      fs.Position := 0;
    end
  finally
    ms.Free;
    fs.Free;
  end;
end;

function GetCertficateInfo(
  var ACertInfo: TStampInfo): Boolean;
var
  iCnt: Integer;
begin
  Result := True;
  if ICert <> nil then
  begin
    ACertInfo.PKAlg := FAlgType;
    ACertInfo.PKLength := FPKLength;
    for iCnt := 0 to Length(FPublicKey) - 1 do
    begin
      ACertInfo.PKContent[iCnt] := FPublicKey[iCnt + 1];
    end;
    ACertInfo.EndDate:= ICert.ValidToDate;
    ACertInfo.DispachTime:= ICert.ValidFromDate;
  end
  else  
    result:= False;
end;


function VerifySign(const AFileName: string): Boolean;
 var
   SignedData: ISignedData;
   HashString: WideString;
   ASignedContent: string;
 begin
   Result := True;
   try
     GetCertificate;

     ASignedContent:= ExtractSignedContent(AFileName);

     HashString:= GetFileHash(AFileName);

     SignedData := CoSignedData.Create;
     SignedData.Content := HashString;  

     SignedData.Verify(ASignedContent, False, CAPICOM_VERIFY_SIGNATURE_ONLY);
   except  
     Result := False;  
     Raise Exception.Create('????????!');
   end;
 end;

procedure form33.GetCertificate;
var
  vStore: TStore;
  iCnt: Integer;
  BaseIntf: IInterface;
  ICert2Dsp: ICertificate2Disp;
begin
  if ICert2 = nil then
  begin
    vStore := OpenStore(FStoreName);
    for iCnt := 1 to vStore.Certificates.Count do
    begin
      IBaseIntf := vStore.Certificates.Item[iCnt];
      try
        if IBaseIntf.QueryInterface(ICertificate2Disp, ICert2Dsp) = 0
        then
        begin
          //????????
          if ICert2Dsp.HasPrivateKey then
          begin
            //???????CSP???
            if ((FProviderName = CSPProvider_ePass) and
                ((ICert2Dsp.PrivateKey.ProviderName = CSPProvider_ePass_1K) or
                 (ICert2Dsp.PrivateKey.ProviderName = CSPProvider_ePass_3K)))
               or (ICert2Dsp.PrivateKey.ProviderName = FProviderName)  
            then  
            begin
              IBaseIntf.QueryInterface(IID_ICertificate2, ICert2);  
              IBaseIntf.QueryInterface(IID_ICertificate, ICert);  
              FPublicKey:= ICert2Dsp.publickey.EncodedKey.Format(True);  
              FPKLength:= ICert2Dsp.publickey.Length;
              FAlgType:= ICert2Dsp.publickey.Algorithm.FriendlyName;  
            end;  
          end;  
        end;  
      except
        //?????CAPICOM?,?????   
        ICert2 := nil;  
      end;  
    end;  
  end;
end;

Última edición por Casimiro Notevi fecha: 24-09-2014 a las 17:04:32.
Responder Con Cita