-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToyCipher.cpp
194 lines (185 loc) · 5.17 KB
/
ToyCipher.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include<bits/stdc++.h>
using namespace std;
class ToyCipher{
int SBox[16] = {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7};
int Permutation[16] = {1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16};
vector<int> MasterKey;
void GenerateMasterKey(){
while(MasterKey.size()<20){
srand(time(0));
int a = rand();
while(a!=0 && MasterKey.size()<20){
MasterKey.push_back(a%2);
a/=2;
}
}
}
string encrypt16Bit(string s){
string result = "";
GenerateMasterKey();
int arr[16];
int k=0;
//convert string into hexadecimal
for(int j=0;j<2;j++){
int m = int(s[j]);
while(k<(j+1)*8){
arr[k++] = m%2;
m/=2;
}
}
for(int round=0;round<4;round++){
// key mixing
for(int j=0;j<16;j++) arr[j]=arr[j]^MasterKey[j+round];
//Substitution
for(int j=0;j<16;j+=4){
int t = 0;
for(int k=j+3;k>=j;k--){
t = t*2 + arr[k];
}
t = SBox[t];
for(int k=j;k<j+4;k++){
arr[k] = t%2;
t = t/2;
}
}
//Permutation
if(round!=3){
int copyarr[16];
for(int j=0;j<16;j++) copyarr[j] = arr[j];
for(int j=0;j<16;j++) arr[j] = copyarr[Permutation[j]-1];
} //key mixing in last round
else{
for(int j=0;j<16;j++) arr[j]=arr[j]^MasterKey[j+4];
}
}
for(int j=0;j<16;j+=8){
int m = 0;
for(int p = j+7;p>=j;p--) m = m*2 + arr[p];
result.push_back(char(m));
}
return result;
}
string decrypt16Bit(string s){
string result = "";
int invSBox[16];
int invPermutation[16];
for(int i=0;i<16;i++){
invSBox[SBox[i]] = i;
invPermutation[Permutation[i]-1] = i;
}
int arr[16];
int k=0;
//convert string into hexadecimal
for(int j=0;j<2;j++){
int m = int(s[j]);
if(m<0)
m+=256;
while(k<(j+1)*8){
arr[k++] = m%2;
m/=2;
}
}
for(int round=3;round>=0;round--){
// key mixing
if(round!=3){
int copyarr[16];
for(int j=0;j<16;j++) copyarr[j] = arr[j];
for(int j=0;j<16;j++) arr[j] = copyarr[invPermutation[j]];
} //key mixing in last round
else{
for(int j=0;j<16;j++) arr[j]=arr[j]^MasterKey[j+4];
}
//SBOX
for(int j=0;j<16;j+=4){
int t = 0;
for(int k=j+3;k>=j;k--){
t = t*2 + arr[k];
}
t = invSBox[t];
for(int k=j;k<j+4;k++){
arr[k] = t%2;
t = t/2;
}
}
for(int j=0;j<16;j++) arr[j]=arr[j]^MasterKey[j+round];
}
for(int j=0;j<16;j+=8){
int m = 0;
for(int p = j+7;p>=j;p--) m = m*2 + arr[p];
result.push_back(char(m));
}
return result;
}
public:
string Encrypt(string s){
string result = "";
for(int i=0;i<s.length();i+=2){
string b = "";
b+=s[i];
if(i==s.length()-1){ //odd length string
result+=b;//to store additional character's encyption so that it can be used while decrypting
}
else{
b+=s[i+1];
b = encrypt16Bit(b);
result+=b;
}
}
return result;
}
string Decrypt(string s){
string result = "";
for(int i=0;i<s.length();i+=2){
string b = "";
b+=s[i];
if(i==s.length()-1){ //odd length string
result+=b;
}
else{
b+=s[i+1];
b = decrypt16Bit(b);
result+=b;
}
}
return result;
}
};
int main(){
ToyCipher T;
ifstream infile("test.txt");
ofstream outfile("encrypted.txt");
if(infile){
string s="";
while(getline(infile, s)){
string result = T.Encrypt(s);
outfile << result;
if(!infile.eof()) {
outfile << endl;
}
}
}
else{
cout<<"file not found";
}
infile.close();
outfile.close();
// Decryption
ifstream in2file("encrypted.txt");
ofstream out2file("decrypted.txt");
if(in2file){
string s="";
while(getline(in2file, s)){
string result = T.Decrypt(s);
out2file << result;
if(!in2file.eof()) {
out2file << endl;
}
}
}
else{
cout<<"file not found";
}
in2file.close();
out2file.close();
return 0;
}