Michael Grenzer
Michael Grenzer

Reputation: 501

Delphi Reverse Decrypt Function

I have the following code in delphi to decrypt:

      new_size := Length(Source) - 1;
      for c := 24 to new_size - 1 do
        begin
          v := Source[c];
          v := v - key;
          v := v AND $FF;
          x := v XOR (Source[c + 1]);
          Source[c] := x;
        end;

where

      Source: Array of Byte;

      Key: Byte;

Any one a quick idea how to reverse this (encrypt again) ?

Upvotes: 1

Views: 371

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

This code is the inverse of the code in the question:

for c := new_size - 1 downto 24 do
  Source[c] := (Source[c] xor Source[c+1]) + key;

And the code in the question can be written much more simply like this:

for c := 24 to new_size - 1 do
  Source[c] := (Source[c] - key) xor Source[c+1];

Upvotes: 7

Related Questions