-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneByteEncoder.cpp
154 lines (123 loc) · 3.26 KB
/
OneByteEncoder.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include<Windows.h>
#include <stdlib.h>
using namespace std;
struct FileInfo
{
LPVOID fileData = 0;
DWORD fileSize = 0;
}ReadedFile;
void usage() {
printf("\nOne Byte Encription Usage\n\npython3 oneByteEncription[-e / -d] File_Path -k key\n\n- e = > Encode\n- d = > Decode\n- k = > key\n\noutput file name : oneByteEncoder.output\n\nAuthor : Murat ERDEM\nSite : muraterdem.org\n\n");
}
void fileReader(char* filename) {
HANDLE fileHandle;
fileHandle = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle == INVALID_HANDLE_VALUE) {
if (GetLastError() == 2) {
cout << "File not found." << endl;
}
else {
cout << "Open file failed. Error Code: " << GetLastError();
}
exit(3);
}
DWORD fileSize = GetFileSize(fileHandle, NULL);
LPVOID fileData = HeapAlloc(GetProcessHeap(), 0, fileSize);
if (fileData == NULL) {
cout << "No Memory Reserved for File." << endl;
exit(3);
}
DWORD bytesRead;
bool r = ReadFile(fileHandle, fileData, fileSize, &bytesRead, NULL);
ReadedFile.fileSize = fileSize;
ReadedFile.fileData = fileData;
}
void fileWrite(char * data) {
HANDLE file = CreateFile(L"oneByteEncoder.output", GENERIC_READ | GENERIC_WRITE, 0, NULL, 2, 0x80, NULL);
DWORD written;
bool donen = WriteFile(file, data, ReadedFile.fileSize, &written, NULL);
CloseHandle(file);
}
void OneByteEncoder(char* filename, int key) {
fileReader(filename);
char* data;
data = new char [ReadedFile.fileSize];
data = (char*)ReadedFile.fileData;
for (int i = 0; i < ReadedFile.fileSize; i++) {
data[i] = data[i] ^ key;
key = data[i];
}
fileWrite(data);
}
void OneByteDecoder(char* filename, int key) {
fileReader(filename);
char* data;
data = new char[ReadedFile.fileSize];
data = (char*)ReadedFile.fileData;
int deger;
for (int i = 0; i < ReadedFile.fileSize; i++) {
deger = data[i];
data[i] = data[i] ^ key;
key = deger;
}
fileWrite(data);
}
int main(int argc, char* argv[])
{
int key, operationType = 0;
char* p;
bool singleUse = false;
char* FileName=0;
HANDLE fileHandle;
cout << "\nAuthor: https://github.com/moncasp \n\n";
if (argc != 5) {
usage();
return 0;
}
else {
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-k") == 0) {
if (i != argc - 1) {
long conv = strtol(argv[4], &p, 10);
key = conv;
}
else usage();
}
if (strcmp(argv[i], "-e") == 0) {
if (operationType == 0) {
operationType = 1;
if (i != argc - 1) FileName = argv[i + 1];
else usage();
}
else {
cout << "The -e and -d parameters cannot be used together\n\n";
exit(3);
}
}
if (strcmp(argv[i], "-d") == 0) {
if (operationType == 0) {
operationType = 2;
if (i != argc - 1) FileName = argv[i + 1];
else usage();
}
else {
cout << "The -e and -d parameters cannot be used together\n\n";
exit(3);
}
}
}
if (operationType == 1) {
printf("One Byte Encoder Started....\n");
OneByteEncoder(FileName, key);
printf("One Byte Encoder Is Over\n");
}
else if (operationType == 2) {
printf("One Byte Decoder Started....\n");
OneByteDecoder(FileName, key);
printf("One Byte Decoder Is Over\n");
}
else cout << "Use one of the -e and -d parameters\n";
}
return 0;
}