forked from olbat/binsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.c
82 lines (70 loc) · 1.71 KB
/
encrypt.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright (C) 2013 Sarzyniec Luc <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* see the COPYING file for more informations */
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PRINT_FORMAT "\\x%.2x"
#define BUFF_SIZE 1024
#define PRINT_DISPLAY(F,C) __extension__ \
({ \
printf(F,C); \
fflush(stdout); \
})
static int fd;
int main(int argc, char **argv)
{
char *key,*ptr;
unsigned char c;
unsigned int i;
size_t len;
if (argc < 2)
{
printf("usage: %s <file> [<key>|-]\n",*argv);
return 1;
}
ptr = 0;
if ((argc > 2) && (strcmp(argv[2],"-")))
{
key = argv[2];
len = strlen(key);
}
else
{
size_t tmp;
key = malloc(BUFF_SIZE);
len = 0;
ptr = key;
while ((tmp = read(STDIN_FILENO,ptr,BUFF_SIZE)) > 0)
{
len += tmp;
if (tmp == BUFF_SIZE)
key = realloc(key,len+BUFF_SIZE);
ptr = key + len;
}
}
fd = open(argv[1], O_RDONLY);
for (i=0;read(fd,&c,sizeof(c)) > 0;i++)
PRINT_DISPLAY(PRINT_FORMAT,c^key[(i + key[i%len])%len]);
close(fd);
if (ptr)
free(key);
puts("");
return 0;
}