Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created March 17, 2009 08:54
Show Gist options
  • Save jamesu/80410 to your computer and use it in GitHub Desktop.
Save jamesu/80410 to your computer and use it in GitHub Desktop.
#include <stdio.h>
// datas
unsigned char data[] = {0x32, 0x7b, 0xe0, 0x51, 0xee, 0x6b, 0x45};
unsigned char other_data[] = {0x2f, 0x71, 0xf6, 0x3, 0xe0, 0x72, 0x5F};
unsigned char pad[] = {0x56, 0x1e, 0x83, 0x23, 0x97, 0x1b, 0x31};
unsigned char out[7];
// works as both an encryptor and decryptor
void crypt(unsigned char *data, unsigned char *pad, unsigned char *out, int size) {
unsigned char *optr = out;
unsigned char *dptr = data;
unsigned char *pptr = pad;
int count = size;
while (count-- != 0)
*optr++ = *dptr++ ^ *pptr++;
}
// prints the data bytes in a c-like format
void print_data(unsigned char *dat, int size) {
int i;
int buff[7];
unsigned char *ptr = dat;
printf("{");
for (i=0; i<size; i++) {
buff[i] = *ptr++;
printf("0x%x, ", buff[i]);
}
printf("}\n");
}
// entrypoint
int main(int argc, char **argv)
{
out[6] = '\0';
// encode first message
sprintf((char*)out, "decrypt");
decrypt(out, pad, data, 6);
print_data(data, 6);
// decode first message
decrypt(data, pad, out, 6);
printf("First message: %s\n", out);
// encode second message
sprintf((char*)out, "you win");
decrypt(out, pad, other_data, 6);
print_data(other_data, 6);
// decode second message
decrypt(other_data, pad, out, 6);
printf("Second message: %s\n", out);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment