-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
103 lines (74 loc) · 1.8 KB
/
main.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
#include <iostream>
#include <string>
#include <stdio.h>
#include <iterator>
using namespace std;
unsigned char state[256];
int len;
int x=0, y=0;
void stateSwap(int i, int j) {
unsigned char temp;
temp = state[i];
state[i] = state[j];
state[j] = temp;
}
void init(unsigned char key[], int keysize) {
for(int i=0; i<256; i++)
state[i] = i;
int j=0;
unsigned char temp;
for(int i=0; i<256; i++) {
j = (j + state[i] + key[i % keysize]) % 256;
stateSwap(i, j);
}
}
unsigned char prga() {
x = (x + 1) % 256;
y = (y + state[x]) % 256;
stateSwap(x, y);
return state[(state[x] + state[y]) % 256];
}
unsigned char* encode(unsigned char data[]) {
unsigned char *cipher = new unsigned char[sizeof(*data)];
for (int i = 0; i < len; i++) {
cipher[i] = data[i] ^ prga();
}
return cipher;
}
int main() {
unsigned char key[] = {"test"};
int keysize = sizeof(key) - 1;
cout << "KEY: ";
for (int idx=0; idx < keysize; idx++)
printf("%02X ", key[idx]);
cout << endl;
//get source
char source[] = "Hello my dear friend";
len = sizeof(source);
//Initialize generator
init(key, keysize);
//Create data
unsigned char *data = (unsigned char*) source;
unsigned char *cipher = new unsigned char[len];
//print source
cout << "SOURCE: ";
for (int idx=0; idx < len; idx++)
printf("%02X ", data[idx]);
cout << endl;
cipher = encode(data);
//print cipher
cout << "CIPHER: ";
for (int idx=0; idx < len; idx++)
printf("%02X ", cipher[idx]);
cout << endl;
//Initialize generator
init(key, keysize);
unsigned char *resource = new unsigned char[len];
resource = encode(cipher);
//print cipher
cout << "CIPHER: ";
for (int idx=0; idx < len; idx++)
printf("%02X ", resource[idx]);
cout << endl;
return 0;
}