-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilecrypter.c
More file actions
70 lines (60 loc) · 1.53 KB
/
filecrypter.c
File metadata and controls
70 lines (60 loc) · 1.53 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* filecrypter.c
*
* Version: 0.1
* Author : Jens Wirth <jw@snej.de>
*
* This tool encrypts a file using another file as password
*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <jw@snej.de> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
*/
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>
int main(int argc, char **argv)
{
FILE *in = fopen(argv[1], "rb");
FILE *out = fopen(argv[2], "wb");
FILE *pw = fopen(argv[3], "rb");
int inbyte = 0;
int pwbyte = 0;
// check arguments
if (argc != 4 || in == NULL || out == NULL || pw ==NULL)
{
printf("Syntax: filecrypter <INFILE> <OUTFILE> <PWFILE>\n");
return 1;
}
// error if input or password file contain no data
fseek(in, 0, SEEK_END);
fseek(pw, 0, SEEK_END);
if (ftell(in) == 0 || ftell(pw) == 0)
{
printf("INFILE and OUTFILE must contain data\n");
return 1;
}
fseek(pw, 0, SEEK_SET);
fseek(in, 0, SEEK_SET);
// encrypt each byte of input file
while ((inbyte=fgetc(in)) != EOF)
{
// rewind password file
if ((pwbyte=fgetc(pw)) == EOF)
{
fseek(pw, 0, SEEK_SET);
pwbyte = fgetc(pw);
}
// simple XOR encryption
fputc(inbyte ^ pwbyte, out);
}
fclose(pw);
fclose(out);
fclose(in);
return 0;
}