package com.teaAlgo;
class TEA {
// Variables
static int sum = 0x0;
static int delta = 0x9e3779b9;
static int lText = 0x12345678;
static int rText = 0x9abcdef;
static int key[] = {0xa56babcd, 0xffffffff, 0xffffffff, 0xabcdef01};
// Main Method
public static void main(String[] args) {
System.out.println("======= Tiny Encryption Algorithm =======");
// Printing Plain Text
System.out.println(String.format("Plain Text is: "+"0x%x", lText) + "" + String.format("%x", rText));
// Calling Encryption
String encText = encrypt(lText, rText);
lText = Integer.parseInt(encText.substring(String.valueOf(lText).length()),16);
rText = Integer.parseInt(encText.substring(String.valueOf(rText).length()),16);
// Calling Decryption
String decText = decrypt(lText, rText);
lText = Integer.parseInt(decText.substring(String.valueOf(lText).length()),16);
rText = Integer.parseInt(decText.substring(String.valueOf(rText).length()),16);
// Printing Key Used
System.out.println("Key Used: 0xa56babcdffffffffffffffffabcdef01 ");
}
//Encryption Method
public static String encrypt (int lText, int rText) {
sum = 0;
//TEA Algorithm for Encrypting
for(int i =0; i <32; i++) {
lText += ( rText << 4 ) + key[0] ^ rText + sum ^ ( rText >>> 5 ) + key[1];
rText += ( lText << 4 ) + key[2] ^ lText + sum ^ ( lText >>> 5 ) + key[3];
sum += delta;
}
// Printing out Encrypted Cipher Text
System.out.println(String.format("Cipher Text is: "+"0x%x", lText) + "" + String.format("%x", rText));
String encText = Integer.toString(lText,16) + Integer.toString(rText,16);
return encText;
}
//Decryption Method
public static String decrypt (int lText, int rText) {
sum = delta << 5;
// TEA Algorithm for Decrypting
for ( int i=0; i<32; i++) {
rText -= ( lText << 4 ) + key[2] ^ lText + sum ^ ( lText >>> 5 ) + key[3];
lText -= ( rText << 4 ) + key[0] ^ rText + sum ^ ( rText >>> 5 ) + key[1];
sum -= delta;
}
// Printing out Decrypted Text
System.out.println(String.format("Decrypted Text is: "+"0x%x", lText) + "" + String.format("%x", rText));
String decText = Integer.toString(lText,16) + Integer.toString(rText,16);
return decText;
}
}
Decryption must match the plain text and I cannot get it to work. it keeps giving me a different output for the decrypted text