/*****************************************************************************/
/* MXTEA.JAVA  -  XTEA Encryption/Decryption Algorithm                       */
/*                                                                           */
/* (C) TDi GmbH                                                              */
/*                                                                           */
/* Implementation File for JAVA                                              */
/*****************************************************************************/
/* The 'Extended Tiny Encryption Algorithm' (XTEA) by David Wheeler and      */
/* Roger Needham of the Cambridge Computer Laboratory.                       */
/* XTEA is a Feistel cipher with XOR and AND addition as the non-linear      */
/* mixing functions.                                                         */
/* Takes 64 bits (8 Bytes block) of data in Data[0] and Data[1].             */
/* Returns 64 bits of encrypted data in Data[0] and Data[1].                 */
/* Takes 128 bits of key in Key[0] - Key[3].                                 */
/*                                                                           */
/* In the calling routine, declare:                                          */
/*    int[] Key  = new int[4];                                               */
/*    int[] Data = new int[2];                                               */
/*                                                                           */
/* Then call the functions:                                                  */
/*    MxTea.MxApp_Encrypt(Data, Key);                                        */
/*    MxTea.MxApp_Decrypt(Data, Key);                                        */
/*****************************************************************************/


import java.io.IOException;

public class MxTea
{

   public static short MxApp_Encrypt(int[] Data, int[] Key)
   {
      int   sum, delta;
      short cnt, i;
      
      delta = 0x9E3779B9;
      sum   = 0;
      cnt   = 32;
      
      for(i=1; i<=cnt; i++)
      {
         Data[0] = Data[0] + ( (((Data[1] << 4) ^ (Data[1] >>> 5)) + Data[1]) ^ (sum + Key[sum & 3]) );
         sum = sum + delta;
         Data[1] = Data[1] + ( (((Data[0] << 4) ^ (Data[0] >>> 5)) + Data[0]) ^ (sum + Key[sum>>>11 & 3]) );
      }
      return 0;
   }

   public static short MxApp_Decrypt(int[] Data, int[] Key)
   {
      int   sum, delta;
      short cnt, i;
      
      delta = 0x9E3779B9;
      sum   = 0xC6EF3720;
      cnt   = 32;
      
      for(i=1; i<=cnt; i++)
      {
         Data[1] = Data[1] - ( (((Data[0] << 4) ^ (Data[0] >>> 5)) + Data[0]) ^ (sum + Key[sum>>>11 & 3]) );
         sum = sum - delta;
         Data[0] = Data[0] - ( (((Data[1] << 4) ^ (Data[1] >>> 5)) + Data[1]) ^ (sum + Key[sum & 3]) );
      }
      return 0;
   }

}




