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 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include <stdio.h> #include <stdint.h>
#define SHL(x, n) ( ((x) & 0xFFFFFFFF) << n ) #define ROTL(x, n) ( SHL((x), n) | ((x) >> (32 - n)) )
unsigned int xorKey[44] = { 0x04050607, 0x00010203, 0x0C0D0E0F, 0x08090A0B, 0xCD3FE81B, 0xD7C45477, 0x9F3E9236, 0x0107F187, 0xF993CB81, 0xBF74166C, 0xDA198427, 0x1A05ABFF, 0x9307E5E4, 0xCB8B0E45, 0x306DF7F5, 0xAD300197, 0xAA86B056, 0x449263BA, 0x3FA4401B, 0x1E41F917, 0xC6CB1E7D, 0x18EB0D7A, 0xD4EC4800, 0xB486F92B, 0x8737F9F3, 0x765E3D25, 0xDB3D3537, 0xEE44552B, 0x11D0C94C, 0x9B605BCB, 0x903B98B3, 0x24C2EEA3, 0x896E10A2, 0x2247F0C0, 0xB84E5CAA, 0x8D2C04F0, 0x3BC7842C, 0x1A50D606, 0x49A1917C, 0x7E1CB50C, 0xFC27B826, 0x5FDDDFBC, 0xDE0FC404, 0xB2B30907 };
void decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) { unsigned int i; uint32_t v0=v[0], v1=v[1], delta = 0x10325476, sum=delta*num_rounds; for (i=0; i < num_rounds; i++) { v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum >> 11) & 3]); sum -= delta; v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[(sum & 3)]); } v[0]=v0; v[1]=v1; }
void XorRol(uint32_t v[2]) { uint32_t encLow = v[1]; uint32_t encHigh = v[0]; uint32_t orgLow, orgHigh, v6, v7, v8; int i;
for ( i = 43; i >= 0; i-- ) { orgLow = encHigh; v6 = ROTL(orgLow, 1); v7 = ROTL(orgLow, 8) & v6; v8 = v7 ^ ROTL(orgLow, 2); orgHigh = encLow ^ xorKey[i] ^ v8; encHigh = orgHigh; encLow = orgLow; } v[0] = orgLow; v[1] = orgHigh; } int main() { uint32_t v[] = { 0xFDF5C266, 0x7A328286, 0xCE944004, 0x5DE08ADC, 0xA6E4BD0A, 0x16CAADDC, 0x13CD6F0C, 0x1A75D936 }; uint32_t k[4] = { 0x03020100, 0x07060504, 0x0B0A0908, 0x0F0E0D0C }; int i, j; uint32_t teaData[8];
for ( i = 0; i <= 3; i++ ) { decipher(1 << (i + 1), v + i * 2, k); printf("0x%X, 0x%X, ", v[i * 2], v[i * 2 + 1]); teaData[i * 2] = v[i * 2]; teaData[i * 2 + 1] = v[i * 2 + 1]; } puts("\n"); for ( i = 0; i <= 3; i++ ) { XorRol(teaData + i * 2);
} puts("\n"); unsigned char * t = (unsigned char *)&teaData; for ( i = 0; i < 32; i += 4 ) printf("%c%c%c%c", t[i + 3], t[i + 2], t[i + 1], t[i]); return 0; }
|