Alright. This is a pretty simple program. The program simply reads a simulated DRM (basically an encrypted plain text file) file that was encrypted using RC4. It then reads a plain text file that contains the exact same plain text that the DRM file would decrypt too.
(This is assuming, however, that the key of the DRM file is long enough so that it creates a key stream long enough such that it has an equal or greater number of bytes than the plain text.
Anyhow. We want to be able to recover the key stream of this .DRM file. To do so, we need to XOR it with the known plain text. This is where we are having issues. We are trying to grab each individual byte of the plain text and then XOR it with the next byte of the cipher text. We then use Strncat to concatenate all of the XORed bytes together and then print them all. For some reason we are not able to print all of the words. Can anyone help us out. I think that the implementation of strncat is the issue.
Thank you in advance.
The relevant text is bolded.
/*
* getc(f): returns a byte of data from file f (EOF means end of file)
* putchar(c): displays a byte (char) onto the screen
* fopen("filename", "r"): opens "filename" for reading
* fclose(f): closes file f
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char K[256][256];
char temp[2];
int c, i, j, k, t, N, x;
FILE *f, *p;
if(argc!= 3)
printf("Usage: %s <drm file> <plaintext>\n", argv[0]);
// drm file
if((f = fopen(argv[1], "r")) == NULL)
{
printf("Error opening %s\n", argv[1]);
return 1;
}
// plain text
if((p = fopen(argv[2], "r")) == NULL){
printf("Error opening %s\n", argv[2]);
return 1;
}
[B] k=0;
while(((c = getc(f)) != EOF) && (t = getc(p)) != EOF){
K[k][0] = '\0';
temp[0] = c ^ t & 0xFF;
temp[1] = '\0';
strncat(K[k], temp, 1);
if(t == ' '|| t == '\n')
k++;
}
for(j=0; j < k; j++){
printf("K[%d]: %s\n\n", j, K[j]);
}[/B]
fclose(p);
fclose(f);
}