简体   繁体   中英

Decryption from encryption method

I am needing to re-write a decryption method for an old piece of code, unfortunately the original decryption method has been lost on we only have access to the encryption.

type
  TintArray = array [0 .. 1] of Cardinal;
  TKeyArray = Array [0 .. 3] of Cardinal;

const
  KeyArray: TKeyArray = (858945348, 1144282739, 828794915, 556884274);

  procedure Encipher(var V, W: TintArray);
  var
    y, z, sum, delta, a, b, c, d, n: Cardinal;
    iCounter: Integer;
  begin
    y := V[0];
    z := V[1];
    sum := 0;
    delta := $9E3779B9; // 2654435769;//0x9E3779B9;
    a := KeyArray[0];
    b := KeyArray[1];
    c := KeyArray[2];
    d := KeyArray[3];
    n := 32;
    for iCounter := n downto 1 do begin
      sum := sum + delta;
      y := y + (((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b));
      z := z + (((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d));
    end;
    W[0] := y;
    W[1] := z;
  end;

I have tried mundane things like changing all the "+" to "-" however I did not have much hope as I really don't actually understand the code at all.

This is the Tiny Encryption Algorithm (TEA). Check it out on Wikipedia .

Your decryption routine should be something like this (keeping with your naming conventions, etc.):

procedure Decipher(var V, W: TintArray);
var
  y, z, sum, delta, a, b, c, d, n: Cardinal;
  iCounter: Integer;
begin
  y := V[0];
  z := V[1];
  sum := $C6EF3720;
  delta := $9E3779B9; // 2654435769;//0x9E3779B9;
  a := KeyArray[0];
  b := KeyArray[1];
  c := KeyArray[2];
  d := KeyArray[3];
  n := 32;
  for iCounter := n downto 1 do begin
    z := z - (((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d));
    y := y - (((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b));
    sum := sum - delta;
  end;
  W[0] := y;
  W[1] := z;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM