This repository has been archived by the owner on Jun 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption_functions.cpp
82 lines (63 loc) · 1.96 KB
/
encryption_functions.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
//////////////////////////////////////////////////
//
// This work is licensed under the MIT license.
//
// You are free to copy, modify and distribute
// this work freely given that proper attribution
// is supplied and the author is not held liable.
// See LICENSE for details.
//
// Copyright (c) 2016-2018 David "OmniTroid" Skoland
//
//////////////////////////////////////////////////
#include "encryption_functions.h"
#include "hex_functions.h"
#include <cstddef>
#include <stdlib.h>
#include <sstream>
#include <iomanip>
#include <QVector>
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);
}