So I've got a program that takes an encrypted file and essentially decrypts it.
I am now trying to modify this file.
During the encrypting process, the words were scrambled by position. The location of each word is marked with a position value.
I decrypted the original text, so now I have the plain text.
In addition, I have the .drm file from which this plain text was deciphered, and I have alice's drm file encrypted using her password.
I want to decrypt alice's .drm. I know that it has the same plaintext as my own. Since she has the same plain text as I do, I know that each word in her plain text is in the same position in the plain text as my own.
Anywho. The first thing that I need to do is take the plaintext that I decrypted and reinsert the position values in front of each word.
I then need to xor this with alice's .drm in order to obtain her keystream.
I need help with these last latter two steps.
I marked off the text where I am trying to read the words from the plain text file and then XOR them with alice's .drm file.
... I also used C++ syntax. I couldn't get c to work, but C++ is close enough
/*
* 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>
#include <ctype.h>
struct WordCatch{
char word[1000];
int lineno;
int position;
};
void wSort(struct WordCatch[], int, int);
int main(int argc, char **argv)
{
struct WordCatch words[256];
unsigned char S[256];
unsigned char K[256];
unsigned char A[256]; // Alice's generated keystream
char buffer1[100];
char buffer2[10];
char passwd[] = "5ne10nemo";
char temp[2];
int c, i, j, k, t, N, x, y, n;
int wordCount = 0;
int lineCount = 1;
int beginning, ending, current;
FILE *f, *a;
if(argc != 3)
{
printf("Usage: %s <YOUdrmFile> <ALICEdrmFile> \n", argv[0]);
return 1;
}
/* File = argv[1] */
if((f = fopen(argv[1], "r")) == NULL)
{
printf("Error opening %s\n", argv[1]);
return 1;
}
/* Password = argv[2] */
if((a = fopen(argv[2], "r")) == NULL){
printf("Error opening %s\n", argv[2]);
return 1;
}
N = strlen(passwd);
for(i = 0; i < 256; i++)
{
S[i] = i;
K[i] = (unsigned char) passwd[i % N];
}
j = 0;
for(i = 0; i < 256; i++)
{
j = (j + S[i] + K[i]) & 0xFF;
t = S[i];
S[i] = S[j];
S[j] = t;
words[i].word[0] = '\0';
words[i].lineno = -1;
words[i].position = -1;
}
i = j = 0;
for (k = 100; k > 1; k--){
//putchar('\n');
}
do
{
c = getc(f);
if(c == 0)
{
k++;
if((c = getc(f)) == EOF)
break;
}
words[wordCount].lineno = k;
words[wordCount].position = c;
//printf("[%d, %d]", k, c);
while((c = getc(f)) != EOF)
{
i = (i + 1) & 0xFF;
j = (j + S[i]) & 0xFF;
t = S[i];
S[i] = S[j];
S[j] = t;
t = (S[i] + S[j]) & 0xFF;
c = c ^ S[t];
temp[0] = c;
temp[1] = '\0';
strncat(words[wordCount].word, temp, 1);
//putchar(c);
if(c == ' ' || c == '\n'){
wordCount++;
//printf("Word %d: %s\n", wordCount-1, words[wordCount-1].word);
break;
}
}
} while(c != EOF);
fclose(f);
/*for(i=0;i<256;i++){
printf("(%d, %d) %s\n", words[i].lineno, words[i].position, words[i].word);
}*/
words[wordCount].lineno = k+1;
wordCount++;
lineCount = 1;
current = 0;
beginning = 0;
ending = wordCount;
//printf("Word count: %d\n", wordCount);
i = j = 0;
-----------------------------------------------------------------------------------
do
{
y = n = 0;
c = getc(a);
if(c == 0){
lineCount++;
if((c = getc(a)) == EOF)
break;
}
while((c = getc(a)) != EOF){
if(isdigit(c)){
x = c;
if(isdigit(c = getc(a)))
x = x*10 + c;
}
printf("X: %d\n", x);[/B]
// look for word in plain text
while(words[y].lineno <= lineCount){
if(words[y++].position == x)
break;
}
A[i++] = c ^ words[y].word[n++];
}
} while(c != EOF);
fclose(f);
for(i = 0; i < 256; i++){
printf("A[%d]: %s\n", i, A[i]);
}
------------------------------------------------------------
// Sort words in word array by line
/*for(lineCount = 1; lineCount < 20; lineCount++){
for(i = current; i < wordCount; i++){
beginning = current;
if(words[i].lineno > lineCount){
ending = i;
//printf("lineno: %d\n", words[i].lineno);
wSort(words, beginning, ending);
current = i;
break;
}
}
}
for(i=0; i<wordCount; i++){
if(words[i].lineno != -1){
//printf("(%d, %d) %s", words[i].lineno, words[i].position, words[i].word);
printf("%s", words[i].word);
}
}*/
}
void wSort(struct WordCatch words[], int beg, int end){
int i;
//printf("Beg: (%d) End: (%d)\n", beg, end);
for(i=beg; i < end; i++){
struct WordCatch temp;
int j, min;
min = i;
for(j = i+1; j < end; j++){
if(words[j].position <= words[min].position)
min = j;
}
temp = words[i];
words[i] = words[min];
words[min] = temp;
}
return;
}