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.
Please submit your code and a short description of how you solved the problem to Gradescope before class. 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.
Submission requirements summary:
Autograder considerations:
For reference, the homework was generated using the following C program running on a little-endian Ubuntu system. Careful: If you try to run it on a Mac, you will discover that the BSD C library implements rand using a different function. This function is equally insecure but you will need to use an Ubuntu docker or VM if your solution uses C rand() to get the same sequence of random numbers as the machine that encrypted the homework. Similarly, the WSL random implementation does not include a initstate function. It is recommended that you run this exercise on Ubuntu (native, VM, or docker).
#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);
}