-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor-usecase.cpp
63 lines (44 loc) · 1.64 KB
/
xor-usecase.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
#include <iostream>
#include <array>
#include "workbench.hpp"
#define XOR_BLOCK_SIZE (8)
std::array<uint8_t, XOR_BLOCK_SIZE> xorbuff(const uint8_t* data, const uint8_t* key) {
std::array<uint8_t, XOR_BLOCK_SIZE> result;
for (size_t i = 0; i < XOR_BLOCK_SIZE; i++)
result[i] = data[i] ^ key[i];
return result;
}
int main() {
std::cout << "This example performs data encoding using the key.\r\nPlease don't confuse it with encryption, XOR ops are only the part of encryption algorithms\r\n\r\n";
std::array<uint8_t, XOR_BLOCK_SIZE> data = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'};
std::array<uint8_t, XOR_BLOCK_SIZE> key = {'k', 'e', 'y', '_', 'h', 'e', 'r', 'e'};
// print data
std::cout << "Data:\r\n";
print_hex(data.data(), XOR_BLOCK_SIZE);
std::cout << "\r\n";
print_binary(data.data(), XOR_BLOCK_SIZE);
std::cout << "\r\n\r\n";
// print key
std::cout << "Key:\r\n";
print_hex(key.data(), XOR_BLOCK_SIZE);
std::cout << "\r\n";
print_binary(key.data(), XOR_BLOCK_SIZE);
std::cout << "\r\n\r\n";
// XOR and print
auto xored = xorbuff(data.data(), key.data());
std::cout << "XORed data:\r\n";
print_hex(xored.data(), XOR_BLOCK_SIZE);
std::cout << "\r\n";
print_binary(xored.data(), XOR_BLOCK_SIZE);
std::cout << "\r\n\r\n";
// XOR XORed data with the key again to restore it
auto restored = xorbuff(xored.data(), key.data());
std::cout << "Restored data:\r\n";
print_hex(restored.data(), XOR_BLOCK_SIZE);
std::cout << "\r\n";
print_binary(restored.data(), XOR_BLOCK_SIZE);
std::cout << "\r\n\r\n";
// report results
std::cout << ((restored == data) ? "Data matched" : "ERROR: Data didn't match") << "\r\n";
return 0;
}