forked from jedisct1/c-ipcrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt_cmd.c
52 lines (45 loc) · 1006 Bytes
/
encrypt_cmd.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
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include "ipcrypt.h"
int
main(int argc, char *argv[])
{
unsigned char k[16];
unsigned int i;
if ( argc == 1 ) {
srand((unsigned int)time(NULL));
for (i = 0; i < 16; i++) {
k[i] = rand() % 255 + 1;
}
}
if ( argc == 2 ) {
strcpy(k, argv[1]);
}
unsigned char ip[4] = {0};
unsigned char out[4];
char ch;
size_t index = 0;
while(read(STDIN_FILENO, &ch, 1) > 0) {
if (isdigit(ch)) {
ip[index] *= 10;
ip[index] += ch - '0';
}
else if (ch == '.' && ip[0] != 0 && index <= 3) { // We found a '.', we are indexing numbers and we don't have 4 indexes already
index++;
}
else if (index > 2) { // no digit, no '.' and our ip is filled
ipcrypt_encrypt(out, ip, k);
printf("%d.%d.%d.%d%c",out[0],out[1],out[2],out[3],ch);
index = 0;
memset(ip, 0, 4);
}
else {
printf("%c",ch);
}
}
return 0;
}