CIS 556, Fall 2016
Homework 2: The C rand() stream cipher

The GNU C Library's implementation of the C rand() function uses a linear congruential generator if the state is initialized with sufficiently small parameters. (It is no longer the default.) You can find the relevant code here.

The LaTeX source for your next homework assignment has been encrypted using C rand() as a stream cipher. The encrypted file is available here. Your task is to recover the plaintext so you can do the rest of the problems.

You may use any programming language you like.

Please submit your code and a short description of how you solved the problem along with a PDF of your LaTeXed solutions to the other problems to Canvas before class on September 20. You may discuss this assignment in small groups with classmates, but please code and write up your solutions yourself. Please credit any collaborators you discussed with and any references you used.

For reference, the homework was generated using the following C program running on a little-endian Ubuntu system. If you try to run it on a Mac, you will discover that the BSD C library implements rand using a different function, which remains equally insecure for our purposes.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

//Return a byte at a time of the rand() keystream
char randchar() { 
  static int key;
  static int i = 0;
  i = i % 4;
  if (i == 0) key = rand();
  return ((char *)(&key))[i++];
}

int main(int argc, const char* argv[]) {
  static char randstate[64];
  initstate(time(NULL),randstate,31);

  FILE *input, *output;
  input = fopen("hw2.tex", "r");
  output = fopen("hw2.tex.enc", "w");

  int c;
  while ((c = fgetc(input)) != EOF)
    fputc(c^randchar(),output);

  fclose(input);
  fclose(output);
}