Ver Mensaje Individual
  #2  
Antiguo 27-06-2024
Avatar de Casimiro Noteví
Casimiro Noteví Casimiro Noteví is offline
Merodeador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.671
Reputación: 10
Casimiro Noteví Tiene un aura espectacularCasimiro Noteví Tiene un aura espectacular
"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