/* * TEA.java * * Created on October 16, 2006, 10:06 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package net.forcedata.common; import java.io.*; public class TEA { int[] key_array = new int[4]; int[] num_array = new int[2]; int[] encrypted_array = new int[2]; int sum = 0; int iteration_step = 32; int delta = 0x9E3779B9; public TEA() { key_array[0] = 442323; key_array[1] = 1231; key_array[1] = 5664; key_array[1] = 674323; //unsigned int32 key[4] = {442323,1231,5664,674323}; } public void encipher() { int a,b,c,d,y, z; int n = iteration_step; { a = key_array[0]; b = key_array[1]; c = key_array[2]; d = key_array[3]; y = num_array[0]; z = num_array[1]; } while (n-- > 0) { sum += delta; y += (z << 4) + (int)Math.pow(a,z) + (int)Math.pow(sum,(z >>> 5)) + b; z += (y << 4) + (int)Math.pow(c,y) + (int)Math.pow(sum,(y >>> 5)) + d; } encrypted_array[0] = y; encrypted_array[1] = z; System.out.println("*************After encryption************"); System.out.println(y + "" + z); } public void decipher() { int sum = 0xC6EF3720; int a,b,c,d,y, z; int n = iteration_step; { a = key_array[0]; b = key_array[1]; c = key_array[2]; d = key_array[3]; y = encrypted_array[0]; z = encrypted_array[1]; } while (n-- > 0) { z -= (y << 4) + (int)Math.pow(c,y) + (int)Math.pow(sum,(y >>> 5)) + d; y -= (z << 4) + (int)Math.pow(a,z) + (int)Math.pow(sum,(z >>> 5)) + b; sum -= delta; } System.out.println("*************After decryption************"); System.out.println(y + "" + z); } /* public void getKey() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int count = 0, idx = 0; try { String str = br.readLine(); while (count <= 3) { key_array[count++] = Integer.parseInt(str.substring(idx, idx + 2)); idx += 2; } } catch (IOException ex) { ex.printStackTrace(); } catch (NumberFormatException ex) { ex.printStackTrace(); } } */ public void getNumber(String number) { //BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int count = 0, idx = 0; try { //String number = br.readLine(); while (count <= 1) { num_array[count++] = Integer.parseInt(number.substring(idx, idx + 2)); idx += 2; } } /* catch (IOException ex) { ex.printStackTrace(); } */ catch (NumberFormatException ex) { ex.printStackTrace(); } } /* public static void main(String[] args) { TEA tea = new TEA(); System.out.println("Enter the key"); tea.getKey(); System.out.println("Enter the number"); tea.getNumber(); tea.encipher(); tea.decipher(); } * * */ }