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
| #include <stdio.h> #include <string.h>
#define DELTA 0x9e3779b9 unsigned char key[16];
void scramble(unsigned int k[], unsigned int t[]) { unsigned int t0 = t[0], t1 = t[1]; unsigned int n, sum = 0; for (n = 0; n < 32; n++) { sum += DELTA; t0 += ((t1 << 4) + k[0]) ^ (t1 + sum) ^ ((t1 >> 5) + k[1]); t1 += ((t0 << 4) + k[2]) ^ (t0 + sum) ^ ((t0 >> 5) + k[3]); } t[0] = t0; t[1] = t1; }
void encrypt(unsigned int k[], char *buf, int len) { int i; for (i = 0; i < len / 8; i++) scramble(k, (unsigned int *)(buf + (i * 8))); }
void descramble(unsigned int k[], unsigned int t[]) { unsigned int t0 = t[0], t1 = t[1]; unsigned int n, sum = DELTA << 5; for (n = 0; n < 32; n++) { t1 -= ((t0 << 4) + k[2]) ^ (t0 + sum) ^ ((t0 >> 5) + k[3]); t0 -= ((t1 << 4) + k[0]) ^ (t1 + sum) ^ ((t1 >> 5) + k[1]); sum -= DELTA; } t[0] = t0; t[1] = t1; }
void decrypt(unsigned int k[], char *buf, int len) { int i; for (i = 0; i < len / 8; i++) descramble(k, (unsigned int *)(buf + (i * 8))); }
int main() { char *plain = "Fuzzing consists of repeatedly testing an application with modified, or fuzzed input"; char text[256]; strcpy(key, "this is a key"); strcpy(text, plain);
encrypt((unsigned int *)key, text, sizeof(text)); printf("encrypt text: %s\n", text); decrypt((unsigned int *)key, text, sizeof(text)); printf("decrypt text: %s\n", text); return 0; }
|