Ver Mensaje Individual
  #3  
Antiguo 28-06-2024
Galahad Galahad is offline
Miembro
 
Registrado: abr 2007
Posts: 266
Reputación: 20
Galahad Va por buen camino
Muchas gracias Casimiro
Se que la longitud tiene que ser de 16 caracteres
voy a seguir peleandome porque no lo consigo, ahora me da un access violation
este es el código de la función con la que estoy trabajando (en base a la que habias puesto)

Código Delphi [-]
function Decrypt(Astr, apikey, apisecret: string): string;
var
  d, s: string;
  iv: array[0..15] of Byte;
  DCP_rijndael: TDCP_rijndael;
  DecodedStream, DecryptedStream: TMemoryStream;
  Len:Integer;
  Padding : char;
begin
  Result := '';
  //if Length(apisecret) <> 16 then
  //  raise Exception.Create('API secret must be 16 characters long.');

  // Decode the base64-encoded string
  d := Base64Decodestr(Astr);
  Len := 16;
  Padding := '$';
  apikey := Format('%-*s', [Len, apikey]).Replace(' ', Padding);
  if Length(apikey) > Len then
    apikey := Copy(apikey, 1, Len);
  apisecret := Format('%-*s', [Len, apisecret]).Replace(' ', Padding);
  if Length(apisecret) > Len then
    apisecret := Copy(apisecret, 1, Len);

  // Create and initialize the Rijndael (AES) cipher
  DCP_rijndael := TDCP_rijndael.Create(nil);
  try
    DCP_rijndael.Algorithm := 'AES/CBC/PKCS5PADDING';
    DCP_rijndael.CipherMode := cmCBC;

    // Set up the initialization vector (IV)
    FillChar(iv, SizeOf(iv), 0); // Assuming iv is just zeros
    Move(apikey[1], iv, Min(Length(apikey), SizeOf(iv)));

    // Initialize the cipher with the key and IV
    DCP_rijndael.Init(apisecret[1], 128, @iv);
    // Decrypt the data
    DecodedStream := TMemoryStream.Create;
    try
      DecodedStream.WriteBuffer(Pointer(d)^, Length(d));
      DecodedStream.Position := 0;

      DecryptedStream := TMemoryStream.Create;
      try
        DCP_rijndael.DecryptStream(DecodedStream, DecryptedStream, DecodedStream.Size);
        SetLength(s, DecryptedStream.Size);
        DecryptedStream.Position := 0;
        DecryptedStream.ReadBuffer(Pointer(s)^, DecryptedStream.Size);
        Result := s;
      finally
        DecryptedStream.Free;
      end;
    finally
      DecodedStream.Free;
    end;
  finally
    DCP_rijndael.Free;
  end;
end;

Cita:
Empezado por Casimiro Notevi Ver Mensaje
"Range check error" suele indicar que estás accediendo a una posición de memoria que no es válida, pueden ser varios motivos:
Asegúrate de que las cadenas tienen la longitud correcta y gestiona bien el IV (vector de inicialización), el CBC, el IV debe tener la longitud correcta.
Un ejemplo de código que tenía guardado por aqui de alguna vez que me ha hecho falta:
Código Delphi [-]
uses
  DCPcrypt2, DCPblockciphers, DCPaes, SysUtils, Classes;

function Decrypt(Astr, apikey, apisecret: string): string;
var
  d, s: string;
  iv: array[0..15] of Byte;
  DCP_rijndael: TDCP_rijndael;
  DecodedStream, DecryptedStream: TMemoryStream;
begin
  Result := '';
  if Length(apisecret) <> 16 then
    raise Exception.Create('API secret must be 16 characters long.');

  // Decode the base64-encoded string
  d := Base64Decodestr(Astr);

  // Create and initialize the Rijndael (AES) cipher
  DCP_rijndael := TDCP_rijndael.Create(nil);
  try
    // Set up the initialization vector (IV)
    FillChar(iv, SizeOf(iv), 0); // Assuming iv is just zeros
    Move(apikey[1], iv, Min(Length(apikey), SizeOf(iv)));

    // Initialize the cipher with the key and IV
    DCP_rijndael.Init(apisecret[1], 128, @iv);

    // Decrypt the data
    DecodedStream := TMemoryStream.Create;
    try
      DecodedStream.WriteBuffer(Pointer(d)^, Length(d));
      DecodedStream.Position := 0;

      DecryptedStream := TMemoryStream.Create;
      try
        DCP_rijndael.DecryptStream(DecodedStream, DecryptedStream, DecodedStream.Size);
        SetLength(s, DecryptedStream.Size);
        DecryptedStream.Position := 0;
        DecryptedStream.ReadBuffer(Pointer(s)^, DecryptedStream.Size);
        Result := s;
      finally
        DecryptedStream.Free;
      end;
    finally
      DecodedStream.Free;
    end;
  finally
    DCP_rijndael.Free;
  end;
end;
Responder Con Cita