-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_encryption.cpp
More file actions
104 lines (85 loc) · 2.44 KB
/
bit_encryption.cpp
File metadata and controls
104 lines (85 loc) · 2.44 KB
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
#include "bit_encryption.h"
#include <algorithm>
#include <random>
constexpr uint8_t BitEncryption::signature[BitEncryption::signatureSize];
std::vector<uint8_t> BitEncryption::encrypt(const std::vector<uint8_t> &originalBytes, const std::vector<uint8_t> &keys)
{
if (originalBytes.empty() || keys.empty())
{
return {};
}
const size_t dataSize = originalBytes.size();
const size_t keyQuantity = keys.size();
std::vector<uint8_t> effectiveKeys(dataSize, 0);
if (keyQuantity >= dataSize)
{
for (size_t k = 0; k < keyQuantity; ++k)
{
effectiveKeys[k % dataSize] ^= keys[k];
}
}
else
{
for (size_t i = 0; i < dataSize; ++i)
{
effectiveKeys[i] = keys[i % keyQuantity];
}
}
std::vector<uint8_t> encryptedBytes(dataSize + signatureSize);
for (size_t i = 0; i < dataSize; ++i)
{
encryptedBytes[i] = static_cast<uint8_t>(originalBytes[i] ^ effectiveKeys[i]);
}
std::reverse(encryptedBytes.begin(), encryptedBytes.begin() + dataSize);
std::copy(signature, signature + signatureSize, encryptedBytes.end() - signatureSize);
return encryptedBytes;
}
std::vector<uint8_t> BitEncryption::decrypt(const std::vector<uint8_t> &encryptedBytes, const std::vector<uint8_t> &keys)
{
if (keys.empty() || encryptedBytes.size() < static_cast<size_t>(signatureSize + 1))
{
return {};
}
const size_t totalSize = encryptedBytes.size();
const size_t dataSize = totalSize - static_cast<size_t>(signatureSize);
if (!std::equal(encryptedBytes.begin() + dataSize, encryptedBytes.end(), signature))
{
return {};
}
std::vector<uint8_t> bytes(encryptedBytes.begin(), encryptedBytes.begin() + dataSize);
std::reverse(bytes.begin(), bytes.end());
const size_t keyQuantity = keys.size();
std::vector<uint8_t> effectiveKeys(dataSize, 0);
if (keyQuantity >= dataSize)
{
for (size_t k = 0; k < keyQuantity; ++k)
{
effectiveKeys[k % dataSize] ^= keys[k];
}
}
else
{
for (size_t i = 0; i < dataSize; ++i)
{
effectiveKeys[i] = keys[i % keyQuantity];
}
}
std::vector<uint8_t> originalBytes(dataSize);
for (size_t i = 0; i < dataSize; ++i)
{
originalBytes[i] = static_cast<uint8_t>(bytes[i] ^ effectiveKeys[i]);
}
return originalBytes;
}
std::vector<uint8_t> BitEncryption::generateKeys(const size_t count)
{
std::random_device rd;
std::uniform_int_distribution<int> dist(0, 255);
std::vector<uint8_t> keys;
keys.reserve(count);
for (size_t i = 0; i < count; ++i)
{
keys.push_back(static_cast<uint8_t>(dist(rd)));
}
return keys;
}