diff options
| author | oldmud0 <oldmud0@users.noreply.github.com> | 2019-01-02 09:40:38 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-02 09:40:38 -0600 |
| commit | 4d93f059c01664ad074cf467e79a63b85e86beb0 (patch) | |
| tree | 57b8a9760796937e66e393b3bc00f4bee4337bdf /src/encryption_functions.cpp | |
| parent | 6f1bce5882676ea7affe717a2f5a00b8c3b7fe12 (diff) | |
| parent | ec1057b5d7e1d39963275bc2cbd79a53cf6f3f5c (diff) | |
Merge pull request #54 from OmniTroid/master
Restructuring + better cross-platform support
Diffstat (limited to 'src/encryption_functions.cpp')
| -rw-r--r-- | src/encryption_functions.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/encryption_functions.cpp b/src/encryption_functions.cpp new file mode 100644 index 00000000..ffbe0cdb --- /dev/null +++ b/src/encryption_functions.cpp @@ -0,0 +1,63 @@ +#include "encryption_functions.h" + +#include "hex_functions.h" + +QString fanta_encrypt(QString temp_input, unsigned int p_key) +{ + //using standard stdlib types is actually easier here because of implicit char<->int conversion + //which in turn makes encryption arithmetic easier + + unsigned int key = p_key; + unsigned int C1 = 53761; + unsigned int C2 = 32618; + + QVector<uint_fast8_t> temp_result; + std::string input = temp_input.toUtf8().constData(); + + for (unsigned int pos = 0 ; pos < input.size() ; ++pos) + { + uint_fast8_t output = input.at(pos) ^ (key >> 8) % 256; + temp_result.append(output); + key = (temp_result.at(pos) + key) * C1 + C2; + } + + std::string result = ""; + + for (uint_fast8_t i_int : temp_result) + { + result += omni::int_to_hex(i_int); + } + + QString final_result = QString::fromStdString(result); + + return final_result; +} + +QString fanta_decrypt(QString temp_input, unsigned int key) +{ + std::string input = temp_input.toUtf8().constData(); + + QVector<unsigned int> unhexed_vector; + + for(unsigned int i=0; i< input.length(); i+=2) + { + std::string byte = input.substr(i,2); + unsigned int hex_int = strtoul(byte.c_str(), nullptr, 16); + unhexed_vector.append(hex_int); + } + + unsigned int C1 = 53761; + unsigned int C2 = 32618; + + std::string result = ""; + + for (int pos = 0 ; pos < unhexed_vector.size() ; ++pos) + { + unsigned char output = unhexed_vector.at(pos) ^ (key >> 8) % 256; + result += output; + key = (unhexed_vector.at(pos) + key) * C1 + C2; + } + + return QString::fromStdString(result); + +} |
