TEA encryption and decryption implementation

0

I have written both encryption and decryption methods to be able to encrypt plain text or decrypt cipher text. I am unsure how to successfully implement this, however, due to taking in the HEX and text as a STRING and unsure how to convert them to int arrays, and then how to successfully print the result out without it being a bunch of jumbled letters. What am I doing incorrectly?

The following code is the two TEA methods for either encryption or decryption:

public void encrypt (int[] block, int[] key) {
    int i = block[0];
    int j = block[1];
    int sum = 0;
    int delta = 0x9e3779b9;

    for (int k = 0; k < 32; k++) {
        sum += delta;
        i += (j << 4 & 0xffffff0) + key[0] ^ j + sum ^ (j >> 5 & 0x7fffffff)
                + key[1];
        j += (i << 4 & 0xfffffff0) + key[2] ^ i + sum ^ (i >> 5 & 0x7ffffff) 
                + key[3]; 
    }

    block[0] = i;
    block[1] = j;
}

public void decrypt (int[] block, int[] key) {
    int i = block[0];
    int j = block[1];
    int sum = 0;
    int delta = 0x9e3779b9;

    for (int k = 0; k < 32; k++) {
        i -= (i << 4 & 0xfffffff0) + key[2] ^ i + sum ^ (i >> 5 & 0x7ffffff)
                + key[3];
        j -= (j << 4 & 0xfffffff0) + key[0] ^ j + sum ^ (j >> 5 & 0x7ffffff)
                + key[1];
        sum -= delta;
    }

    block[0] = i;
    block[1] = j;
}

and this is the user interface code:

public void encryptionT() {
    p.plainText();
    String pText = input.next();
    p.hexNumber();
    String hexNum = input.next();
    byte hex[] = hexNum.getBytes();
    byte pTextBytes[] = pText.getBytes();
    byte[] encryptedPlainBase = Base64.getEncoder().encode(pTextBytes);
    System.out.println(encryptedPlainBase);

}

public void decryptionT() {
    p.cipherText();
    String cText = input.next();
    p.hexNumber();
    String hexNum = input.next();
    byte hex[] = hexNum.getBytes();
    byte cTextBytes[] = cText.getBytes();
    BigInteger hexBigInt = new BigInteger(1, hex);
    BigInteger plainBigInt = new BigInteger(1, cTextBytes);
    BigInteger cTextHexResult = hexBigInt.multiply(plainBigInt);
    byte[] decryptedCipherText = cTextHexResult.toByteArray();
    byte[] decryptedCipherBase = Base64.getDecoder().decode(decryptedCipherText);
    System.out.println(decryptedCipherBase);

EDIT:

I attempted to do the suggested edits but I get the following error. I'm not sure if I just didn't understand.enter image description here

java
encryption
asked on Stack Overflow Jan 25, 2018 by Kelsie Tomerlin • edited Jan 28, 2018 by Kelsie Tomerlin

1 Answer

0

Wow, this is the first time I've seen an implementation of encryption / decryption where the actual encryption / decryption is forgotten entirely.


I'll explain about the key first - usually you start with the key, not the plaintext as the key can commonly be reused for other messages. TEA however only accepts a key, not an IV or nonce, so encrypting multiple messages with the same key will break the cipher.

The hexadecimal key must first be decoded to binary. However, you need a hexadecimal decoder for that. This is however not included in the normal Java package java.util - use e.g. the Apache codec library instead. This should leave you with the binary key of half the size you are obtaining now (the binary encoding of the hexadecimal characters).


Now you need to encode the plaintext message to binary - i.e. a byte array in Java, which you are doing in pText.getBytes().

Now you need to create the ciphertext from the plaintext message by encrypting the binary obtained above.

You are correct in seeing that the ciphertext must be encoded if you want to treat it as text. So using a base64 encoding on the result is good practice - unless binary ciphertext would also suffice (Java I/O streams and files are fine with binary values) in which case the encoding is simply not necessary.


Decryption goes the other way except for the key. You first create the key as explained above. Then you decode the base 64, decrypt the binary result and then use the String constructor that takes a byte array to retrieve the plaintext.


Note that String#getBytes and new String(byte[]) use the platform decoding by default. You may want to specify an exact encoding such as UTF-8 to be compatible between platforms.

answered on Stack Overflow Jan 27, 2018 by Maarten Bodewes

User contributions licensed under CC BY-SA 3.0