summaryrefslogtreecommitdiffstats
path: root/read_crypt.c
blob: af3a7c0073ef6c1d83b305b7ed011d6d6b6f6e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char *argv[])	{
//reads a message <4096B and XORs it with key argv[1] in file argv[2]
//message ends with string "@@@" after a white character.
//To retrieve the original message, use onetimebluh.c
FILE * keyfile = fopen(argv[1],"r");
FILE * crypt = fopen(argv[2],"w");
char msg[4096];

printf("Write a message.\n It will be encrypted in file %s ",argv[2]);
printf("with key %s\n",argv[1]);
printf("When you're done, use the EOF sequence @@@. It must follow a white space!\n");

char c_aux;
int i=0,k=0;
//scanf terminates at every white character, must be called by a While
// "i" tells where you are in msg (which is overwritten each time)
// "k" tells where you are in crypt 
while(strcmp(msg,"@@@"))
	{
	scanf("%s",msg);
	i=0;
	while(msg[i]!='\0') {
		fscanf(keyfile,"%c", &c_aux);
		fprintf(crypt,"%c",msg[i]^c_aux);
		i++;
		k++;
		}
	fscanf(keyfile,"%c",&c_aux);
	fprintf(crypt,"%c",' '^c_aux);
	k++;
	}

while (k<4096){
// fills the message with @s 
	fscanf(keyfile,"%c",&c_aux);
	fprintf(crypt,"%c",'@'^c_aux);
	k++;}
fclose(crypt);
}