Translate code

I need (ASAP) to translate the next lines of code to Xojo

class function Encriptar(Encriptar: Boolean; Texto: String): String;
var
I: Integer;
Clave: Word;
const
C1 = 11111;
C2 =22222;
Begin

Clave:= $FFFF;
Result:= Texto;

for I:= 1 to Length(Texto) do
begin
Result[i] := Char(Byte(Texto[I]) xor (Clave shr 8));
if Encriptar then Clave:= (Byte(Result[I]) + Clave) * C1 + C2 // Encriptar
else Clave:= (Byte(Texto[I]) + Clave) * C1 + C2; // Desencriptar
end;
end;

Thanks

What language is this from ?

Looks like Pascal.

I’ll take a shot…

Important to know since the behavior of the runtime and what it does with overflows etc might be important

Function Encriptar(Encriptar As Boolean, Textto As String) As String
  const C1 = 11111
  const C2 = 22222
  
  dim Clave as UInt16 = &hFFFF
  
  dim mbSource as MemoryBlock = Textto
  dim mbResult as new MemoryBlock( mbSource.Size )
  
  dim lastIndex as integer = mbSource.Size - 1
  for byteIndex as integer = 0 to lastIndex
    mbResult.Byte( byteIndex ) = mbSource.Byte( byteIndex ) Xor Bitwise.ShiftRight( Clave, 8 )
    if Encriptar then
      Clave = ( mbResult.Byte( byteIndex ) + Clave ) * C1 + C2 // Encriptar
    else
      Clave = ( mbSource.Byte( byteIndex ) + Clave ) * C1 + C2 // Desencriptar
    end if
  next
  
  return mbResult
End Function

Note that the result will have a nil encoding so it will be up to the caller to define its encoding properly.

thank you very much for your quick response.

It works!

Someone changed my encryption code and now need to use the following which is similar to the above:

thank you very much

function Encriptar(Encriptar: Boolean; Texto: string): string;
var
INMS, OutMS: TMemoryStream;
SStream: TStringStream;
cnt: Integer;
C: Byte;
Clave: Word;
const
C1 = 52845;
C2 = 22719;
begin
INMS := TMemoryStream.Create;
OutMS := TMemoryStream.Create;
Clave := $F14B;
try
INMS := TStringStream.Create(Texto);
INMS.Position := 0;
for cnt := 0 to INMS.Size - 1 do
begin
INMS.Read(C, 1);
C := (Byte© xor (Lo(Clave)));
if Encriptar then
Clave := Word((Byte© xor Lo(Clave)) * C1 + C2) // Encriptar
else
Clave := Word((Byte((Byte© xor (Lo(Clave)))) xor Lo(Clave)) * C1 + C2); // Desencriptar

  OutMS.Write(C, 1);
end;

finally
SStream := TStringStream.Create(’’);
SStream.CopyFrom(OutMS, 0);
Result := SStream.DataString;
INMS.Free;
OutMS.Free;
end;
end;

Solved.

Thanks.