diff options
| author | alpine <[email protected]> | 2020-06-13 22:27:52 +0200 |
|---|---|---|
| committer | alpine <[email protected]> | 2020-06-13 22:27:52 +0200 |
| commit | bad7b4f2d19f95b278fdcb3056be01cae9af1dbb (patch) | |
| tree | 5bef91f910a2c03d74df9693a077ee33b2fe7886 /client/src/util | |
| parent | Initial commit (diff) | |
| download | loader-bad7b4f2d19f95b278fdcb3056be01cae9af1dbb.tar.xz loader-bad7b4f2d19f95b278fdcb3056be01cae9af1dbb.zip | |
Client.
Message encryption.
Packet handler.
Disconnect event handler.
Diffstat (limited to 'client/src/util')
| -rw-r--r-- | client/src/util/xor.cpp | 41 | ||||
| -rw-r--r-- | client/src/util/xor.h | 14 |
2 files changed, 55 insertions, 0 deletions
diff --git a/client/src/util/xor.cpp b/client/src/util/xor.cpp new file mode 100644 index 0000000..483c161 --- /dev/null +++ b/client/src/util/xor.cpp @@ -0,0 +1,41 @@ +#include "../include.h" +#include "xor.h" + +char enc::gen_key() { + std::random_device r; + + std::default_random_engine e1(r()); + std::uniform_real_distribution<> uniform_dist(0, 255); + return static_cast<char>(uniform_dist(e1)); +} + +// XOR keys at the beginning of the message for clients +void enc::encrypt_message(std::string &str) { + std::array<char, key_num> keys; + for (size_t i = 0; i < key_num; i++) { + char key = gen_key(); + keys[i] = key; + str.insert(str.begin(), key); + } + + for (auto &key : keys) { + for (size_t i = key_num; i < str.size(); i++) { + str[i] ^= key; + } + } +} + +// XOR keys at the end of the message for server messages +void enc::decrypt_message(std::string &str) { + if (str.size() <= 50) return; + + std::string keys = str.substr(str.size() - key_num); + + for (auto &key : keys) { + for (size_t i = 0; i < str.size() - key_num; i++) { + str[i] ^= key; + } + } + + str.erase(str.end() - key_num, str.end()); +}
\ No newline at end of file diff --git a/client/src/util/xor.h b/client/src/util/xor.h new file mode 100644 index 0000000..1ae1ce2 --- /dev/null +++ b/client/src/util/xor.h @@ -0,0 +1,14 @@ +#pragma once + +namespace enc { +constexpr size_t key_num = 50; + +char gen_key(); + +// XOR keys at the beginning of the message for clients +void encrypt_message(std::string &str); + +// XOR keys at the end of the message for server messages +void decrypt_message(std::string &str); + +} // namespace enc
\ No newline at end of file |