-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkappyCLI.cpp
184 lines (161 loc) · 4.29 KB
/
SkappyCLI.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
#include <iostream>
#include <cryptopp/sha.h>
#include <fstream>
#include <cryptopp/cryptlib.h>
#include <cryptopp/rijndael.h>
#include <cryptopp/modes.h>
#include <cryptopp/files.h>
#include <cryptopp/osrng.h>
#include <cryptopp/hex.h>
#include <chrono>
/*
void loadfile()
{
std::string path;
std::cout << "gib path: ";
std::cin >> path;
std::ifstream inputfile(path, std::ifstream::binary);
while (inputfile.good())
{
char block[1024];
std::streamsize length = 1024;
inputfile.read(block, length);
std::cout.write(block, inputfile.gcount());
}
inputfile.close();
}
*/
//#define DEBUG
#ifdef DEBUG
std::string Path = "C:\\Users\\RSKALA\\Desktop\\salam.txt";
std::string Key = "THISISAKEY";
#endif // DEBUG
CryptoPP::SecByteBlock GenerateKey(std::string PassPhrase)
{
using namespace CryptoPP;
SHA256 HashObj;
std::string Hash;
StringSource(PassPhrase, true, new HashFilter(HashObj, new StringSink(Hash)));
SecByteBlock KeyHashed(reinterpret_cast<const byte*>(&Hash[0]), Hash.size());
return KeyHashed;
}
CryptoPP::SecByteBlock GenerateIv()
{
using namespace CryptoPP;
AutoSeededRandomPool prng;
SecByteBlock Iv(AES::BLOCKSIZE);
prng.GenerateBlock(Iv, Iv.size());
return Iv;
}
void WriteIvToFileBeginning(CryptoPP::SecByteBlock* Iv, std::ofstream* OutputFile)
{
using namespace CryptoPP;
ArraySource(*Iv, Iv->size(), true, new FileSink(*OutputFile));
}
bool IsSkappyFile(std::string& Path)
{
//https://www.techiedelight.com/extract-n-characters-from-the-end-of-a-string-in-cpp/
if (Path.size() < 7) {
return false;
}
if (Path.substr(Path.size() - 7) == ".skappy")
{
return true;
}
else
{
return false;
}
}
void BeginEncrypt()
{
system("cls");
#ifndef DEBUG
std::string Path;
std::cout << "Ok! Now drag and drop the file in here so i can encrypt it: ";
std::cin >> Path;
std::string Key;
std::cout << "Now give me the password that you want to encrypt the file with: ";
std::cin >> Key;
auto start = std::chrono::high_resolution_clock::now();
#endif
using namespace CryptoPP;
SecByteBlock Iv = GenerateIv();
SecByteBlock KeyHashed = GenerateKey(Key);
CFB_Mode<AES>::Encryption EncryptObj;
EncryptObj.SetKeyWithIV(KeyHashed, KeyHashed.size(), Iv);
std::ifstream InputFile(Path, std::ios_base::binary);
std::ofstream OutputFile(Path + ".skappy", std::ios_base::binary);
WriteIvToFileBeginning(&Iv, &OutputFile);
FileSource Encryption(InputFile, false, new StreamTransformationFilter(EncryptObj, new FileSink(OutputFile)));
while (!InputFile.eof() && !Encryption.SourceExhausted())
{
Encryption.Pump(1024);
}
OutputFile.close();
InputFile.close();
#ifdef DEBUG
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
std::cout << duration.count() << std::endl;
#endif //
}
void BeginDecrypt()
{
system("cls");
#ifndef DEBUG
std::string Path;
do
{
std::cout << "Ok! Now drag and drop the file in here so i can decrypt it (make sure its a .skappy file): ";
std::cin >> Path;
} while (!IsSkappyFile(Path));
std::string Key;
std::cout << "Now give me the password that you encrypted the file with: ";
std::cin >> Key;
#endif
using namespace CryptoPP;
SecByteBlock KeyHashed = GenerateKey(Key);
SecByteBlock Iv(AES::BLOCKSIZE);
std::ifstream InputFile(Path, std::ios_base::binary);
std::ofstream OutputFile(Path.substr(0,Path.size() - 7), std::ios_base::binary);
InputFile.read((char*)& Iv[0], 16);
CFB_Mode<AES>::Decryption DecryptObj;
DecryptObj.SetKeyWithIV(KeyHashed, KeyHashed.size(), Iv);
FileSource Encryption(InputFile, false, new StreamTransformationFilter(DecryptObj, new FileSink(OutputFile)));
while (!InputFile.eof() && !Encryption.SourceExhausted())
{
Encryption.Pump(1024);
}
OutputFile.close();
InputFile.close();
}
void PrintWelcome()
{
system("cls");
using namespace std;
cout << "Hello and welcome to Skappy. Skappy will encrypt whatever file you desire with AES-CFB-256 to make sure your file is secure." << endl;
cout << "Please select an option:" << endl << "[1] Encrypt\n[2] Decrypt\n[3] Exit\n";
}
int main()
{
int Option;
do
{
PrintWelcome();
std::cin >> Option;
} while (Option > 3 && Option < 0);
switch (Option)
{
case 1:
BeginEncrypt();
break;
case 2:
BeginDecrypt();
break;
case 3:
return 0;
break;
}
return 0;
}