-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmltool.c
90 lines (68 loc) · 2.32 KB
/
xmltool.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
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
extern int crypt_decrypt_file2buffer_C(int fd, char *buffer);
extern int rand(void);
void logstr(char *msg)
{
fprintf(stdout, "%s\n", msg);
fflush(stdout);
}
int main(int argc, const char *argv[])
{
int input_fd = 0;
int output_fd = 0;
int fileSize = 0;
void *handle = NULL;
int decryptResult = 0;
if (argc < 3){
logstr("Usage: ./xmltool <e|d> input.xml output.xml");
logstr(" e = Encrypt Mode. -> output.xml should be a copy of encrypted file");
logstr(" d = Decrypt Mode.");
return 1;
}
char* operation = argv[1];
char* inputFileName = argv[2];
char* outputFileName = argv[3];
input_fd = open(inputFileName, O_RDONLY);
if(input_fd == 0) {
logstr("Failed to open input file");
return 1;
}
fileSize = lseek(input_fd, 0, SEEK_END);
lseek(input_fd, 0, SEEK_SET);
fprintf(stdout, "Input File Size : %d\n", fileSize);
// fprintf(stdout, "Current Pointer : %d\n", ftell(input_fd));
fflush(stdout);
if (operation[0] == 'd') {
output_fd = open(outputFileName, O_WRONLY | O_CREAT);
logstr("Decrypting file");
char* output_buff = (char *)malloc(fileSize + 0x10);
decryptResult = crypt_decrypt_file2buffer_c(input_fd, output_buff);
if (decryptResult > 0)
write(output_fd, output_buff, decryptResult);
} else if (operation[0] == 'e') {
output_fd = open(outputFileName, O_RDWR, 0666);
logstr("Encrypting file");
char* input_buff = (char *)malloc(fileSize);
read(input_fd, input_buff, fileSize);
char* key_info = (char *)malloc(0x60);
lseek(output_fd, 100, SEEK_SET);
read(output_fd, key_info, 0x60);
ftruncate(output_fd, 200);
lseek(output_fd, 0, SEEK_SET);
decryptResult = crypt_encrypt_buffer2file_c(input_buff, fileSize, output_fd);
//Fix encryption keys
lseek(output_fd, 100, SEEK_SET);
write(output_fd, key_info, 0x60);
}
if (decryptResult < 0)
{
fprintf(stderr, "Decryption / Encryption error: %s %d\n", inputFileName, decryptResult);
return -1;
}
close(output_fd);
fprintf(stdout, "Decryption result written to %s", outputFileName);
close(input_fd);
return 0;
}