TEA
TEA
在加密的过程中要加密的明文使用2个32位无符号整数(2×4字节),秘钥为4个32位无符号整数(4×4字节),更长的明文可通过分为多个4字节为单位的小组分别进行加密(循环)
在逆向分析实战中判断TEA
算法的可从其3行核心加密中出现的右移4左移5,两行各有3个小括号互相异或的加密流程和单次加密循环32次以及运算中出现的sum
和delta
变量看出
下面是TEA
算法加密过程的C语言代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <stdio.h> #include <stdint.h> void encrypt(uint32_t* temp, uint32_t* key) { uint32_t v0 = temp[0], v1 = temp[1]; int sum = 0; uint32_t delta = 0x9e3779b9; for (int i = 0; i < 32; i++) { v1 += ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3]); v0 += ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1]); sum += delta; } temp[0] = v0; temp[1] = v1; }
int main() { uint32_t key[4] = { }; uint32_t temp[2]; uint8_t flag[32] = { }; for (int i = 0; i < 32; i += 8) { temp[0] = *(uint32_t*)&flag[i]; temp[1] = *(uint32_t*)&flag[i + 4];
encrypt(temp, key);
for (int j = 0; j < 2; j++) { for (int m = 0; m < 4; m++) { printf("%c", temp[j] & 0xff); temp[j] >>= 8; } } } return 0; }
|
下面是TEA
算法解密过程的C语言代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <stdio.h> #include <stdint.h> void decrypt(uint32_t* temp, uint32_t* key) { uint32_t v0 = temp[0], v1 = temp[1]; int sum = 0x9e3779b9 * 32; uint32_t delta = 0x9e3779b9; for (int i = 0; i < 32; i++) { v1 -= ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3]); v0 -= ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1]); sum -= delta; } temp[0] = v0; temp[1] = v1; }
int main() { uint32_t key[4] = { }; uint32_t temp[2]; uint8_t flag[32] = { };
for (int i = 0; i < 32; i += 8) { temp[0] = *(uint32_t*)&flag[i]; temp[1] = *(uint32_t*)&flag[i + 4];
decrypt(temp, key);
for (int j = 0; j < 2; j++) { for (int m = 0; m < 4; m++) { printf("%c", temp[j] & 0xff); temp[j] >>= 8; } } } return 0; }
|