-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.cpp
97 lines (89 loc) · 2.09 KB
/
encrypt.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <vector>
#include "round.h"
#include "block.h"
#include "key.h"
#include "encrypt.h"
using namespace std;
const int INVERSE_IP[] = {
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
Encrypt::Encrypt(string plainText, string providedKey) {
int size = plainText.length();
int index = 0;
int key[48]; // use get key(key#, key), key# starts at 1 through 16
Key k(providedKey);
if(size % 16 != 0) {
plainText.insert(plainText.length(), "0000000000000000", (16 - (size % 16)));
}
while(index != plainText.length()) {
Rn.clear();
Ln.clear();
RL.clear();
char tmp[16];
for(int i = 0; i < 16; i++) {
tmp[i] = plainText.at(i + index);
}
Block blk(tmp);
Ln = blk.getLeft();
Rn = blk.getRight();
for(int j = 1; j <= 16; j++) {
k.getKey(j, key);
Round r(Ln, Rn, key);
Ln = Rn;
Rn = r.getRight();
}
for(int j = 0; j < 32; j++) {
RL.push_back(Rn[j]);
}
for(int j = 0; j < 32; j++) {
RL.push_back(Ln[j]);
}
for(int j = 0; j < 64; j++) {
inverseIP.push_back(RL[INVERSE_IP[j] - 1]);
}
index += 16;
}
}
string Encrypt::getEncrypted() {
int tmpChar[4];
C = "";
for(int i = 0; i < inverseIP.size(); i++) {
tmpChar[i % 4] = inverseIP[i];
if(i % 4 == 3) {
C = C + convertToHex(tmpChar);
}
}
return C;
}
char Encrypt::convertToHex(int x[4]) {
int y = (x[0] * 8) + (x[1] * 4) + (x[2] * 2) + (x[3] * 1);
char ch;
switch(y) {
case 0 : ch = '0'; break;
case 1 : ch = '1'; break;
case 2 : ch = '2'; break;
case 3 : ch = '3'; break;
case 4 : ch = '4'; break;
case 5 : ch = '5'; break;
case 6 : ch = '6'; break;
case 7 : ch = '7'; break;
case 8 : ch = '8'; break;
case 9 : ch = '9'; break;
case 10 : ch = 'A'; break;
case 11 : ch = 'B'; break;
case 12 : ch = 'C'; break;
case 13 : ch = 'D'; break;
case 14 : ch = 'E'; break;
case 15 : ch = 'F'; break;
}
return ch;
}