-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.cpp
186 lines (170 loc) · 3.92 KB
/
aes.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
#include "aes.h"
void Aes::aes_enc() {
int extendedlength=0;
string myText;
ifstream File;
string filepath = "encryption.aes";
File.open(filepath.c_str(), std::ifstream::out | std::ifstream::trunc );
if (!File.is_open() || File.fail())
{
File.close();
printf("\nError : failed to erase file content !");
}
File.close();
fstream newfile;
newfile.open("input.txt",ios::in);
if (newfile.is_open()){
sleep(1);
string tp;
sleep(1);
sleep(1);
sleep(1);
while(getline(newfile, tp)){
int messlength=tp.length();
int extendedlength;
if((messlength%16)!=0)
{
extendedlength=messlength+(16-(messlength%16));
}
else
{
extendedlength=messlength;
}
unsigned char* encryptedtext=new unsigned char[extendedlength];
for(int i=0;i<extendedlength;i++)
{
if(i<messlength)
encryptedtext[i]=tp[i];
else
encryptedtext[i]=0;
}
string k;
ifstream infile;
infile.open("key.txt");
if (infile.is_open())
{
getline(infile, k);
infile.close();
}
istringstream tempkey(k);
unsigned char key[16];
unsigned int x;
for(int i=0;i<16;i++)
{
tempkey>>hex>>x;
key[i] = x;
}
unsigned char extendedkeys[176];
Key_extenxion(key,extendedkeys);
for(int i=0;i<extendedlength;i+=16)
{
unsigned char* temp=new unsigned char[16];
for(int j=0;j<16;j++)
{
temp[j]=encryptedtext[i+j];
}
encryption(temp , extendedkeys);
for(int j=0;j<16;j++)
{
encryptedtext[i+j]=temp[j];
}
}
ofstream fout;
ifstream fin;
fin.open("encryption.aes");
fout.open ("encryption.aes",ios::app);
if(fin.is_open())
fout<<encryptedtext<<"\n";
fin.close();
fout.close();
}
newfile.close();
}
}
void Aes::aes_dec() {
int extendedlength=0;
string myText;
sleep(1);
string tp;
sleep(1);
sleep(1);
sleep(1);
ifstream File;
string filepath = "outputtext.txt";
File.open(filepath.c_str(), std::ifstream::out | std::ifstream::trunc );
if (!File.is_open() || File.fail())
{
File.close();
printf("\nError : failed to erase file content !");
}
File.close();
ifstream MyReadFile;
MyReadFile.open("encryption.aes", ios::in | ios::binary);
if(MyReadFile.is_open())
{
while( getline (MyReadFile, myText) )
{
cout.flush();
char * x;
x=&myText[0];
int messlength=strlen(x);
char * msg = new char[myText.size()+1];
strcpy(msg, myText.c_str());
int n = strlen((const char*)msg);
unsigned char * decryptedtext = new unsigned char[n];
for (int i = 0; i < n; i++) {
decryptedtext[i] = (unsigned char)msg[i];
}
string k;
ifstream infile;
infile.open("key.txt");
if (infile.is_open())
{
getline(infile, k);
infile.close();
}
else cout << "Unable to open file";
istringstream tempkey(k);
unsigned char key[16];
unsigned int x1;
for(int i=0;i<16;i++)
{
tempkey>>hex>>x1;
key[i] = x1;
}
unsigned char extendedkeys[176];
Key_extenxion(key,extendedkeys);
for (int i = 0; i < messlength; i += 16)
{
unsigned char * temp=new unsigned char[16];
for(int j=0;j<16;j++)
temp[j]=decryptedtext[i+j];
decryption(temp , extendedkeys);
for(int j=0;j<16;j++)
decryptedtext[i+j]=temp[j];
}
for(int i=0;i<messlength;i++)
{
cout<<decryptedtext[i];
if(decryptedtext[i]==0 && decryptedtext[i-1]==0 )
break;
}
cout<<endl;
ofstream fout;
ifstream fin;
fin.open("outputtext.txt");
fout.open ("outputtext.txt",ios::app);
if(fin.is_open())
fout<<decryptedtext<<"\n";
fin.close();
fout.close();
sleep(5);
}
}
else
{
cout<<"Can not open input file\n ";
}
cout<<"\n Data has been appended to file outputtext.txt";
MyReadFile.close();
}