Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Coloboración Paypal con ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #3  
Antiguo 28-06-2024
Galahad Galahad is offline
Miembro
 
Registrado: abr 2007
Posts: 240
Poder: 19
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
 



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
DEPLOY ANDROID 64 BITS SOLUCIÓN A [PAClient Error] Error: E6408 Y SIMILARES dani36652 Desarrollo en Delphi para Android 5 31-01-2020 22:11:37
Intraweb - Database Server Error: SQL State:08001, SQL Error Code: 17 bitbow Delphi para la web 2 12-11-2015 22:28:15
ibase_query() [function.ibase-query]: Dynamic SQL Error SQL error code = -104 Token MALBOTO22 PHP 8 06-05-2015 19:22:39
Encriptación y desencriptación de datos mcalmanovici Varios 9 15-07-2008 15:34:47
Encriptacion/Desencriptacion de Archivo TXT Maury Manosalva OOP 9 22-09-2005 02:04:55


La franja horaria es GMT +2. Ahora son las 16:11:21.


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