-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.cpp
More file actions
82 lines (78 loc) · 2.5 KB
/
Process.cpp
File metadata and controls
82 lines (78 loc) · 2.5 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
#include "Process.h"
void Process::encryption(char plaintext[], char chipertext[], PassWord passWordSet)
{
///Convert char to int format
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
this->plaintext[i * 8 + j] = (plaintext[i] >> j) & 1;
}
}
initialPermutation(this->plaintext);
for(int i = 0; i < 16; ++i) {
roundFunction(this->plaintext, passWordSet.pwdSet[i]);
}
for(int i = 0; i < 32; ++i) {
swap(this->plaintext[i], this->plaintext[i + 32]);
}
inverseInitialPermutation(this->plaintext);
///Conver int to char format
for(int i = 0; i < 8; ++i) {
for(int j = 0; j < 8; ++j) {
if(this->plaintext[i * 8 + j]) {
chipertext[i] |= (1 << j);
} else {
chipertext[i] &= ~(1 << j);
}
}
}
}
void Process::initialPermutation(int plaintext[])
{
int temp[64];
for(int i = 0; i < 64; ++i) {
temp[i] = plaintext[IP_Table[i]];
}
copy(plaintext, plaintext + 64, temp);
}
void Process::inverseInitialPermutation(int plaintext[])
{
int temp[64];
for(int i = 0; i < 64; ++i) {
temp[i] = plaintext[IPInverse_Table[i]];
}
copy(plaintext, plaintext + 64, temp);
}
///The total number of round is 16.This is one round.
void Process::roundFunction(int plaintext[], int password[])
{
int newL[32];
int newR[32];
int oldL[32];
int oldR[32];
copy(this->plaintext, this->plaintext + 32, oldL);
copy(this->plaintext + 32, this->plaintext + 64, oldR);
copy(oldR, oldR + 32, newL); ///no need. just convenient for comprehend
int RE[48];
for(int i = 0; i < 48; ++i) { ///Expand 32 bits to 48 bits
RE[i] = oldR[E_Table[i] - 1];
}
for(int i = 0; i < 48; ++i) { ///Substiution 48 bits to 32 bits
RE[i] ^= password[i];
}
for(int i = 0; i < 8; ++i) { ///S-box operation
int row = 2 * RE[i * 6] + RE[i * 6 + 5];
int col = 8 * RE[i * 6 + 1] + 4 * RE[i * 6 + 2]
+ 2 * RE[i * 6 + 3] + RE[i * 6 + 4];
int answer = S_Table[i][row * 16 + col];
for(int j = 0; j < 4; ++j) {
oldR[i * 4 + 3 - j] = answer % 2;
answer /= 2;
}
}
for(int i = 0; i < 32; ++i) {
newR[i] = oldR[P_Table[i] - 1];
newR[i] ^= oldL[i];
}
copy(newL, newL + 32, this->plaintext);
copy(newR, newR + 32, this->plaintext + 32);
}