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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <stdio.h> #include <stdlib.h> unsigned int delta[] = { 0x26, 0x1e27, 0x52f6, 0x985, 0x2297, 0x2e15, 0x20ad, 0x7e1d, 0x28d2, 0x7794, 0x16dd, 0x6dc4, 0x476, 0x119, 0x5039, 0x3e31, 0x22f1, 0x66ad, 0xbb5, 0x3958, 0x51f0, 0x7c93, 0x5497, 0x6532, 0x4819, 0x52b, 0x70d1, 0x8c0, 0x25fd, 0x7e16, 0x98e, 0x24e, 0x348, 0x489b, 0x420b, 0x52f5, 0x5c3b, 0x3149, 0x30a8, 0x363, 0x735d, 0x1ade, 0x6e3f, 0x45df, 0x7b6d, 0x5068, 0x2fb4, 0x7987, 0x1d9a, 0x42aa, 0x1dcd, 0x72dc, 0x2ff7, 0x34c1, 0x5f44, 0x2d81, 0x3029, 0x1c08, 0x91b, 0x4b40, 0x5662, 0x3738, 0x6930, 0x44e, 0x5494, 0x20d4, 0x5f11, 0x6cd0, 0x15de, 0x60c4, 0x3711, 0x339d, 0x124b, 0x413f, 0x3b9c, 0x3e46, 0xabb, 0x6aef, 0x70c7, 0x4654, 0x4121, 0xc50, 0x2e2b, 0x5bd0, 0xef, 0x105a, 0xaf4, 0x7109, 0xbcf, 0x285f, 0x5035, 0x5391, 0x3e94, 0x2d36, 0x657f, 0x3689, 0x270, 0x1b99, 0x6bb1, 0x321e, 0x5e67, 0x2fcc, 0x7a11, 0x5c54, 0x3d03, 0x647f, 0x319c, 0x5f03, 0x3a4a, 0x58f6, 0x1a9b, 0x2f1e, 0xded, 0x6267, 0x77, 0x493b, 0x65c2, 0x4ca4, 0x3fce, 0x1750, 0x4474, 0xdf9, 0x3ac6, 0x63bb, 0x387a, 0x7258, 0x67a2, 0x7d86 }; unsigned int sum[] = { 0x6f0f9, 0x7d7e9, 0x76142, 0x873fc };
void TEA_encrypt(unsigned int* v, const unsigned int* k) { unsigned int v0 = v[0], v1 = v[1]; unsigned int delta = 0x61C88647, sum = 0;
for (int i = 0; i < 32; ++i) { delta = rand(); printf("0x%x, ", delta); sum += delta; v0 += (k[1] + (v1 >> 5)) ^ (sum + v1) ^ (k[0] + (v1 << 4)); v1 += (k[3] + (v0 >> 5)) ^ (sum + v0) ^ (k[2] + (v0 << 4)); }
v[0] = v0; v[1] = v1; printf("0x%x,", sum); }
void TEA_decrypt(unsigned int* v, int* k, int round) { unsigned int v0 = v[0], v1 = v[1];
for (int i = 31; i > -1; i--) { v1 -= (k[3] + (v0 >> 5)) ^ (sum[round] + v0) ^ (k[2] + (v0 << 4)); v0 -= (k[1] + (v1 >> 5)) ^ (sum[round] + v1) ^ (k[0] + (v1 << 4)); sum[round] -= delta[round * 32 + i]; }
v[0] = v0; v[1] = v1; }
int main() { srand(0); unsigned int last[9] = { 0xEA2063F8, 0x8F66F252, 0x902A72EF, 0x411FDA74, 0x19590D4D, 0xCAE74317, 0x63870F3F, 0xD753AE61,0 }; int key[4] = { 2, 2, 3, 3 };
int round = 3; for (int i = 6; i > -1; i -= 2) {
TEA_decrypt(&last[i], key, round); round--; }
printf("\nDecrypted values:\n"); for (int i = 0; i < 8; i += 2) { printf("0x%X 0x%X\n", last[i], last[i + 1]); } printf("%s", (char*)last); return 0; }
|