Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Ayuda a pasar codigo delphi7 a XE3 (https://www.clubdelphi.com/foros/showthread.php?t=87181)

elmago00 25-11-2014 06:39:26

Ayuda a pasar codigo delphi7 a XE3
 
1 Archivos Adjunto(s)
Hola,
llevo semanas tratando de hacer funcionar bien un código de delphi7 en embarcadero xe3.
pero da la casualidad, que en delphi7 me da un código PIC y en embarcadero me genera otro

Código Delphi [-]
function md5(const Input: String): String;
var
  hCryptProvider: HCRYPTPROV;
  hHash: HCRYPTHASH;
  bHash: array[0..$7f] of Byte;
  dwHashBytes: Cardinal;
  pbContent: PByte;
  i: Integer;

begin
  dwHashBytes := 16;
  pbContent := Pointer(PChar(Input));

  Result := '';

  if CryptAcquireContext(@hCryptProvider, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT or CRYPT_MACHINE_KEYSET) then
  begin
    if CryptCreateHash(hCryptProvider, CALG_MD5, 0, 0, @hHash) then
    begin
      if CryptHashData(hHash, pbContent, Length(Input) * sizeof(Char), 0) then
      begin
        if CryptGetHashParam(hHash, HP_HASHVAL, @bHash[0], @dwHashBytes, 0) then
        begin
          for i := 0 to dwHashBytes - 1 do
          begin
            Result := Result + Format('%.2x', [bHash[i]]);
          end;
        end;
      end;
      CryptDestroyHash(hHash);
    end;

    CryptReleaseContext(hCryptProvider, 0);
  end;

  Result := AnsiLowerCase(Result);

  end;


procedure TForm2.Button1Click(Sender: TObject);
var
aHash,S,  aKey : String;
Tmp : array [1..16] of integer;
i:integer;
  begin



aHash := Md5(Edit1.text);



for i:=1 to 16 do Tmp[i]:=StrToInt('$'+rightStr(leftstr(aHash,2*i),2));



aKey:='';
for i:=1 to 8 do aKey:=  aKey + IntToStr((((Tmp[i]+Tmp[i+8])and$FF)*9)div$FF);


while(length(akey)<8) do aKey:='0'+aKey;
memo1.lines.Clear;

Memo1.Lines.Add('PIC:' + akey);

end;

end.

valor : 235641110256249
resultado PIC:47553704 esto en XE3 pero en delphi7 da : PIC:86536540


uso esto
Archivo Adjunto 3068


gracias por la ayuda

ElKurgan 25-11-2014 08:28:31

Todo tiene que ver con que a partir de Delphi 2009 los String y los Char se corresponden con caracteres Unicode, mientras que los antiguos String y Char son ahora AnsiString y AnsiChar.

Prueba con este código, a ver:

Código Delphi [-]
//function md5(const Input: String): String;
function md5(const Input: AnsiString): AnsiString;
var
  hCryptProvider: HCRYPTPROV;
  hHash: HCRYPTHASH;
  bHash: array[0..$7f] of Byte;
  dwHashBytes: Cardinal;
  pbContent: PByte;
  i: Integer;

begin
  dwHashBytes := 16;
//  pbContent := Pointer(PChar(Input));
  pbContent := Pointer(PAnsiChar(Input));

  Result := '';

  if CryptAcquireContext(@hCryptProvider, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT or CRYPT_MACHINE_KEYSET) then
  begin
    if CryptCreateHash(hCryptProvider, CALG_MD5, 0, 0, @hHash) then
    begin
//      if CryptHashData(hHash, pbContent, Length(Input) * sizeof(Char), 0) then
      if CryptHashData(hHash, pbContent, Length(Input) * sizeof(AnsiChar), 0) then
      begin
        if CryptGetHashParam(hHash, HP_HASHVAL, @bHash[0], @dwHashBytes, 0) then
        begin
          for i := 0 to dwHashBytes - 1 do
          begin
            Result := Result + Format('%.2x', [bHash[i]]);
          end;
        end;
      end;
      CryptDestroyHash(hHash);
    end;

    CryptReleaseContext(hCryptProvider, 0);
  end;

  Result := AnsiLowerCase(Result);

  end;

Veras que sólo he sustituido los "Char" por "AnsiChar", y la llamada a la función con AnsiString

Saludos

elmago00 25-11-2014 18:27:05

gracias [ElKurgan] solucionaste el problema


La franja horaria es GMT +2. Ahora son las 13:44:47.

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