#include /* F_k(in) = k ^ in */ void F(unsigned char *key, unsigned char *in, unsigned char *out){ /* assumes |key| = |in| = |out| = 8 bytes */ int i; for (i=0; i<8; i++) out[i] = key[i] ^ in[i]; return; } main(){ FILE *randfile, *fpIn, *fpOut; unsigned char key[8], in[8], out[8], rand[8]; int i; fpIn = fopen("msg.txt", "r"); randfile = fopen("/dev/urandom", "r"); /* don't use /dev/urandom for real-world crypto... */ fpOut = fopen("ctext.txt", "w"); fread(key, 1, 8, randfile); // load a random key while (fread(in, 1, 8, fpIn) == 8) { // read the next block of plaintext fread(rand, 1, 8, randfile); // rand is a random 8-byte string F(key, rand, out); // out = F(key, rand) // encrypt the current plaintext block; // note that each plaintext block corresponds to two ciphertext blocks. // plaintext block m is encrypted as r, F_k(r) ^ m as in class for (i=0; i<8; i++) fprintf(fpOut, "%02X", rand[i]); for (i=0; i<8; i++) fprintf(fpOut, "%02X", in[i]^out[i]); } fclose(fpIn); fclose(randfile); fclose(fpOut); return; }