Reputation: 832
I'm trying to implement TEA (tiny encryption algorithm) in Java, to encode a byte[]
containing audio, of size 512.
Here's my encrypt function:
//Encrypt 64bit/8byte buffer with 128bit/16byte key
public byte[] encrypt(int[] data, int[] key) {
int x = data[0];
int y = data[1];
ByteBuffer encrypted = ByteBuffer.allocate(8);
int sum = 0;
int constant = 0x9e3779b9; //magic constant
for (int k = 0; k < 32; ++k) {
sum += constant;
x += (y << 4 & 0xfffffff0) + key[0] ^ y + sum ^ (y >> 5 & 0x7ffffff) + key[1];
y += (x << 4 & 0xfffffff0) + key[2] ^ x + sum ^ (x >> 5 & 0x7ffffff) + key[3];
}
encrypted.putInt(x);
encrypted.putInt(y);
return encrypted.array();
}
and decrypt:
public byte[] decrypt(int[] data, int[] key) {
int x = data[0];
int y = data[1];
ByteBuffer decrypted = ByteBuffer.allocate(8);
int sum = 0xC6EF3720; //32*delta
int constant = 0x9e3779b9; //magic constant
for (int k = 0; k < 32; ++k) {
x -= (x << 4 & 0xfffffff0) + key[2] ^ x + sum ^ (x >> 5 & 0x7ffffff) + key[3];
y -= (y << 4 & 0xfffffff0) + key[0] ^ y + sum ^ (y >> 5 & 0x7ffffff) + key[1];
sum -= constant;
}
decrypted.putInt(x);
decrypted.putInt(y);
return decrypted.array();
}
and my encrypt call:
ByteBuffer unwrapEncrypt = ByteBuffer.allocate(512);
int[] encryptionKey = {55555, 8888, 123857, 912029};
//block is a byte[] with length 512
ByteBuffer plainText = ByteBuffer.wrap(block);
for (int j = 0; j < block.length / 8; j++) {
//Initiate array for int pairs
int[] plainTextInts = new int[2];
plainTextInts[0] = plainText.getInt();
plainTextInts[1] = plainText.getInt();
//Encrypt and store
unwrapEncrypt.put(encrypt(plainTextInts, encryptionKey));
}
and decrypt call:
ByteBuffer audioToPlay = ByteBuffer.allocate(512);
int[] decryptionKey = {55555, 8888, 123857, 912029};
//audio is a byte[] with length 512
ByteBuffer cipherText = ByteBuffer.wrap(audio);
for (int j = 0; j < audio.length / 8; j++) {
int[] plainTextInts = new int[2];
//Initiate array for int pairs
plainTextInts[0] = cipherText.getInt();
plainTextInts[1] = cipherText.getInt();
//Decrypt and store
audioToPlay.put(decrypt(plainTextInts, decryptionKey));
}
Sorry for the mass of code - I've tried analysis on sent audio and received decrypted data - they're both of correct length, just entirely different. If I remove these 4 blocks of code, the audio is perfect. Can anyone spot what's up? Thanks
Upvotes: 0
Views: 275
Reputation: 3464
I seems there is a bug in your decrpyt()
method when comparing it to Wikipedia' s description of TEA. You have to exchange x and y on the left of the -=
operators. The following seems to work for me:
public byte[] decrypt(int[] data, int[] key) {
int x = data[0];
int y = data[1];
ByteBuffer decrypted = ByteBuffer.allocate(8);
int sum = 0xC6EF3720; //32*delta
int constant = 0x9e3779b9; //magic constant
for (int k = 0; k < 32; ++k) {
y -= (x << 4 & 0xfffffff0) + key[2] ^ x + sum ^ (x >> 5 & 0x7ffffff) + key[3];
x -= (y << 4 & 0xfffffff0) + key[0] ^ y + sum ^ (y >> 5 & 0x7ffffff) + key[1];
sum -= constant;
}
decrypted.putInt(x);
decrypted.putInt(y);
return decrypted.array();
}
Upvotes: 1