|
| 1 | +package com.Brian.TeaProgram; |
| 2 | +//---------------------------------------------------------------------------------------------------------------------- |
| 3 | +// *********************************** FOR LICENSING PLEASE VIEW LICENSING FILE *********************************** |
| 4 | +//---------------------------------------------------------------------------------------------------------------------- |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +// *Algorithm designed and written by: Roger Needham, and David Wheeler (1994-1998)* |
| 17 | +//---------------------------------------------------------------------------------------------------------------------- |
| 18 | +// +====================================+ |
| 19 | +// | Code By: Brian Carey | |
| 20 | +// | Tiny Encryption (TEA) Program | |
| 21 | +// | Updated: 2.19.2020 | |
| 22 | +// | v. 1.0.4 | |
| 23 | +// | https://github.com/Bri-dot-dev | |
| 24 | +// +====================================+ |
| 25 | +//---------------------------------------------------------------------------------------------------------------------- |
| 26 | +// ******************** PLEASE VIEW README FOR INSTRUCTIONS ON HOW TO USE THIS PROGRAM PROPERLY ******************** |
| 27 | +//---------------------------------------------------------------------------------------------------------------------- |
| 28 | +class TEA { |
| 29 | + |
| 30 | + // Variables |
| 31 | + static int sum = 0x0; |
| 32 | + static int delta = 0x9e3779b9; //Constant Variable Delta |
| 33 | + static int lText = 0x01ca4567; //<----- ENTER LEFT HEX HERE (Left Text Block In Hex) |
| 34 | + static int rText = 0x0cabcdef; //<------ENTER RIGHT HEX HERE (Right Text Block In Hex) |
| 35 | + static int key[] = {0xaf6babcd, 0xef00f000, 0xfeafffaf, 0xabcdef01}; //<-- ENTER KEY HERE (128-Bit Key) |
| 36 | + |
| 37 | + |
| 38 | + public static void log(String msg) { |
| 39 | + System.out.println(msg); |
| 40 | + } |
| 41 | + |
| 42 | + public static void main(String[] args) { |
| 43 | + log("+==============================================+"); |
| 44 | + log("|======| Tiny Encryption (TEA) Program |=======|"); |
| 45 | + log("|===============| Brian Carey |================|"); |
| 46 | + log("|==================| v1.0.4 |==================|"); |
| 47 | + log("+==============================================+"); |
| 48 | + // Printing Plain Text |
| 49 | + log(String.format("| Plain Text is: "+"0x%08x", lText) + "" + String.format("%08x |", rText)); |
| 50 | + log( "|----------------------------------------------|"); |
| 51 | + // Calling Encryption |
| 52 | + int[] encText = encrypt(lText, rText); |
| 53 | + // Calling Decryption |
| 54 | + String decText = decrypt(encText[0], encText[1]); |
| 55 | + //Parsing Left Block |
| 56 | + lText = Integer.parseInt(decText.substring(String.valueOf(lText).length()),16); |
| 57 | + //Parsing Right Block |
| 58 | + rText = Integer.parseInt(decText.substring(String.valueOf(rText).length()),16); |
| 59 | + // Printing Key Used |
| 60 | + log(String.format("| Key Used: 0x%08x%08x%08x%08x |", key[0], key[1], key[2], key[3])); |
| 61 | + log("+==============================================+"); |
| 62 | + } |
| 63 | + //Encryption Method |
| 64 | + public static int[] encrypt (int l, int r) { |
| 65 | + // Handling Errors With Key Input |
| 66 | + if( key == null) |
| 67 | + { |
| 68 | + log("Key is not defined!"); |
| 69 | + System.exit(0); |
| 70 | + } |
| 71 | + sum = 0; |
| 72 | + //TEA Algorithm for Encrypting |
| 73 | + for(int i =0; i <32; i++) { |
| 74 | + |
| 75 | + sum += delta; |
| 76 | + |
| 77 | + l += (r << 4 & 0xfffffff0 ) + key[0] ^ r + sum ^ (r >>> 5 & 0x7ffffff) + key[1]; |
| 78 | + |
| 79 | + r += (l << 4 & 0xfffffff0 ) + key[2] ^ l + sum ^ (l >>> 5 & 0x7ffffff) + key[3]; |
| 80 | + |
| 81 | + } |
| 82 | + // Printing out Encrypted Cipher Text |
| 83 | + log(String.format("| Cipher Text is: "+"0x%08x", l) + "" + String.format("%08x |", r)); |
| 84 | + log("|----------------------------------------------|"); |
| 85 | + // Returning "encText" or Encrypted Text |
| 86 | + int[] encText = {l,r}; |
| 87 | + return encText; |
| 88 | + } |
| 89 | + //------------------------------------------------------------------------------------------------------------------ |
| 90 | + //Decryption Method |
| 91 | + public static String decrypt (int l, int r) { |
| 92 | + // Handling Errors With Key Input |
| 93 | + if( key == null) |
| 94 | + { |
| 95 | + log("Key is not defined!"); |
| 96 | + System.exit(0); |
| 97 | + } |
| 98 | + // TEA Algorithm for Decrypting |
| 99 | + sum = delta << 5; |
| 100 | + |
| 101 | + for ( int i=0; i<32; i++) { |
| 102 | + |
| 103 | + r -= (l << 4 & 0xfffffff0) + key[2] ^ l + sum ^ (l >>> 5 & 0x7ffffff) + key[3]; |
| 104 | + |
| 105 | + l -= (r << 4 & 0xfffffff0) + key[0] ^ r + sum ^ (r >>> 5 & 0x7ffffff) + key[1]; |
| 106 | + |
| 107 | + sum -= delta; |
| 108 | + } |
| 109 | + //-------------------------------------------------------------------------------------------------------------- |
| 110 | + // Printing out Decrypted Text |
| 111 | + log(String.format("| Decrypted Text is: "+"0x%08x", l) + "" + String.format("%08x |", r)); |
| 112 | + log("|----------------------------------------------|"); |
| 113 | + // Returning "decText" or Decrypted Text |
| 114 | + String decText = Integer.toString(l,16) + Integer.toString(r,16); |
| 115 | + return decText; |
| 116 | + } |
| 117 | +} |
0 commit comments