-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha51.cpp
More file actions
132 lines (106 loc) · 2.65 KB
/
a51.cpp
File metadata and controls
132 lines (106 loc) · 2.65 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "a51.h"
#include "QDebug"
A51::A51(const QString &key)
{
this->key = QBitArray(64);
this->x = QBitArray(19);
this->y = QBitArray(22);
this->z = QBitArray(23);
for (int i = 0; i < key.length(); i++) {
this->key.setBit(i, key.at(i).toLatin1() == '0' ? false : true);
}
this->resetRegisters();
}
QString A51::runAlgo(const QString &src, bool encrypt)
{
Q_UNUSED(encrypt);
QString res;
this->resetRegisters();
for (int i = 0; i < src.length(); i++) {
res.append(this->stepByStep(src.at(i)));
}
return res;
}
QString A51::stepByStep(const QChar &srcChar){
bool m = this->majorityVote();
bool s;
bool input = srcChar.toLatin1() == '1' ? true : false;
if (this->x.at(8) == m) {
this->stepX();
emit this->xStepped(this->bitArrayToString(this->x));
}
if (this->y.at(10) == m) {
this->stepY();
emit this->yStepped(this->bitArrayToString(this->y));
}
if (this->z.at(10) == m) {
this->stepZ();
emit this->zStepped(this->bitArrayToString(this->z));
}
s = this->x.at(18) ^ this->y.at(21) ^ this->z.at(22);
return (s ^ input) ? "1" : "0";
}
QString A51::returnKey()
{
QString retKey;
for (int i = 0; i < this->key.count(); i++) {
retKey.append(this->key.at(i) ? '1' : '0');
}
return retKey;
}
void A51::stepX()
{
bool t = this->x.at(13) ^ this->x.at(16) ^ this->x.at(17) ^ this->x.at(18);
for (int i = 18; i > 0; i--) {
this->x.setBit(i, this->x.at(i - 1));
}
this->x.setBit(0, t);
}
void A51::stepY()
{
bool t = this->y.at(20) ^ this->y.at(21);
for (int i = 21; i > 0; i--) {
this->y.setBit(i, this->y.at(i - 1));
}
this->y.setBit(0, t);
}
void A51::stepZ()
{
bool t = this->z.at(7) ^ this->z.at(20) ^ this->z.at(21) ^ this->z.at(22);
for (int i = 22; i > 0; i--) {
this->z.setBit(i, this->z.at(i - 1));
}
this->z.setBit(0, t);
}
/**
* @brief A51::majorityVote
* @return
*/
bool A51::majorityVote()
{
int vote = 0;
if (this->x.at(8)) vote++;
if (this->y.at(10)) vote++;
if (this->z.at(10)) vote++;
return vote >= 2;
}
void A51::resetRegisters()
{
for (int i = 0; i < 64; i++) {
if (i < 19) {
this->x.setBit(i, this->key.at(i));
} else if (i < 19 + 22) {
this->y.setBit(i - 19, this->key.at(i));
} else {
this->z.setBit(i - 19 - 22, this->key.at(i));
}
}
}
QString A51::bitArrayToString(const QBitArray &bArr)
{
QString res;
for (int i = 0; i < bArr.size(); i++) {
res.append(bArr.at(i) ? "1" : "0");
}
return res;
}