title: XTEA encryption give different output in C and Delphi
body: <p>I'm coding an application on both C and Delphi, I've XTEA Encryption in both, its exactly the same
  but the problem is the output is different, even though the delta and N is the same.
  I don't really know whats wrong</p>
  
  <p>Here is the Delphi code</p>
  
  <pre><code>type
    TTeaMsgBlock = array[0..1] of LongWord;
    TTeaKeyBlock = array[0..3] of LongWord;
  
  const   
    DELTA = $9e3779b9;
    N = 32;
  
  procedure XTeaCrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
  var
    I: LongWord;
    S: Int64;
  begin
    S := 0;
    for I := 0 to N - 1 do begin
      Inc(V[0], (((V[1] shl 4) xor (V[1] shr 5)) + V[1]) xor (S + K[S and 3]));
      Inc(S, DELTA);
      Inc(V[1], (((V[0] shl 4) xor (V[0] shr 5)) + V[0]) xor (S + K[(S shr 11) and 3]));
    end;
  end;
  
  function XTeaCryptStr(const Msg, Pwd: string): string;
  var
    V: TTeaMsgBlock;
    K: TTeaKeyBlock;
    I, L, N: Integer;
  begin
    L := Length(Pwd); if L &gt; SizeOf(K) then L := SizeOf(K);
    K[0] := 0; K[1] := 0; K[2] := 0; K[3] := 0; Move(Pwd[1], K[0], L);
  
    I := 1; L := Length(Msg);
    if L &gt; 0 then SetLength(Result, ((L - 1) div SizeOf(V) + 1) * SizeOf(V))
             else SetLength(Result, 0);
    while I &lt;= L do begin
      V[0] := 0; V[1] := 0;
      N := L - I + 1; if N &gt; SizeOf(V) then N := SizeOf(V);
      Move(Msg[I], V[0], N);
      XTeaCrypt(V, K);
      Move(V[0], Result[I], SizeOf(V));
      Inc(I, SizeOf(V))
    end;
  end;
  
  //Test
  const Key: array [0..15] of char = (char($00), char($01), char($02), char($03), char($04), char($05), 
                                  char($06), char($07), char($08), char($09), char($0a), char($0b), 
                                  char($0c), char($0d), char($0e), char($0f));
  const Msg: string = 'This Is#';
  
  begin
   WriteLn('Encrypted: ' + pChar(XTeaCryptStr(Msg, Key)));
  end.
  </code></pre>
  
  <p>and thats the C code (Taken From PolarSSL)</p>
  
  <pre><code>typedef struct
  {
      uint32_t k[4];       /*!&lt; key */
  } xtea_context;
  
  #ifndef GET_ULONG_BE
  #define GET_ULONG_BE(n,b,i)                             \
  {                                                       \
      (n) = ( (unsigned long) (b)[(i)    ] &lt;&lt; 24 )        \
          | ( (unsigned long) (b)[(i) + 1] &lt;&lt; 16 )        \
          | ( (unsigned long) (b)[(i) + 2] &lt;&lt;  8 )        \
          | ( (unsigned long) (b)[(i) + 3]       );       \
  }
  #endif
  
  #ifndef PUT_ULONG_BE
  #define PUT_ULONG_BE(n,b,i)                             \
  {                                                       \
      (b)[(i)    ] = (unsigned char) ( (n) &gt;&gt; 24 );       \
      (b)[(i) + 1] = (unsigned char) ( (n) &gt;&gt; 16 );       \
      (b)[(i) + 2] = (unsigned char) ( (n) &gt;&gt;  8 );       \
      (b)[(i) + 3] = (unsigned char) ( (n)       );       \
  }
  #endif
  
  /*
   * XTEA key schedule
   */
  void xtea_setup( xtea_context *ctx, unsigned char key[16] )
  {
      int i;
  
      memset(ctx, 0, sizeof(xtea_context));
  
      for( i = 0; i &lt; 4; i++ )
      {
          GET_ULONG_BE( ctx-&gt;k[i], key, i &lt;&lt; 2 );
      }
  }
  
  /*
   * XTEA encrypt function
   */
  int xtea_crypt_ecb( xtea_context *ctx, int mode, unsigned char input[8],
                       unsigned char output[8])
  {
      uint32_t *k, v0, v1, i;
  
      k = ctx-&gt;k;
  
      GET_ULONG_BE( v0, input, 0 );
      GET_ULONG_BE( v1, input, 4 );
  
      if( mode == XTEA_ENCRYPT )
      {
          uint32_t sum = 0, delta = 0x9E3779B9;
  
          for( i = 0; i &lt; 32; i++ )
          {
              v0 += (((v1 &lt;&lt; 4) ^ (v1 &gt;&gt; 5)) + v1) ^ (sum + k[sum &amp; 3]);
              sum += delta;
              v1 += (((v0 &lt;&lt; 4) ^ (v0 &gt;&gt; 5)) + v0) ^ (sum + k[(sum&gt;&gt;11) &amp; 3]);
          }
      }
      else /* XTEA_DECRYPT */
      {
          uint32_t delta = 0x9E3779B9, sum = delta * 32;
  
          for( i = 0; i &lt; 32; i++ )
          {
              v1 -= (((v0 &lt;&lt; 4) ^ (v0 &gt;&gt; 5)) + v0) ^ (sum + k[(sum&gt;&gt;11) &amp; 3]);
              sum -= delta;
              v0 -= (((v1 &lt;&lt; 4) ^ (v1 &gt;&gt; 5)) + v1) ^ (sum + k[sum &amp; 3]);
          }
      }
  
      PUT_ULONG_BE( v0, output, 0 );
      PUT_ULONG_BE( v1, output, 4 );
      output[8] = '\0';
  
      return( 0 );
  }
  
  //test
  int main()
  {
      int i;
      unsigned char buf[8] = "This Is#";
      xtea_context ctx;
      xtea_setup( &amp;ctx, (unsigned char *) xtea_test_key);
      xtea_crypt_ecb( &amp;ctx, XTEA_ENCRYPT, buf, buf );
      printf("Encrypted = %s\n", buf);
      return 0;
  }
  </code></pre>
  
  <p>but the output is totally different :/
  What am I doing wrong?</p>