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
|
/**
* @file FileManIOFile.cpp
* @brief FileManIOFile class definitions
* @author manzerbredes
* @date 9 Mars 2015
*
* Contain all definitions of FileManIOFile class.
*
*/
#include "FileManIOFile.hpp"
#include <iomanip> // This might be necessary
FileManIOFile::FileManIOFile(std::string filename){
this->filename=filename;
this->readable=false;
this->data="";
this->key;
}
FileManIOFile::~FileManIOFile(){
}
bool FileManIOFile::isReadable(){
return this->readable;
}
void FileManIOFile::read(std::string key){
AESCrypt aes;
HASHCrypt hash;
std::ifstream file;
this->data.clear();
file.open (this->filename, std::ios::in | std::ios::binary);
byte fileMD5[16];
file.read((char*) fileMD5, sizeof(fileMD5));
char car;
file.read(&car, sizeof(car));
while(file){
this->data+=car,
file.read(&car, sizeof(car));
}
this->data=aes.decrypt(key, this->data);
byte currentMD5[16];
hash.getMD5_128(this->data, currentMD5, sizeof(currentMD5));
if(hash.compareDigest(fileMD5, currentMD5, sizeof(currentMD5))){
this->readable=true;
hash.getSHA_256(key, this->key, 32);
}
else{
this->readable=false;
}
file.close();
}
void FileManIOFile::write(std::string key,std::string data){
AESCrypt aes;
HASHCrypt hash;
std::string dataEncrypted;
dataEncrypted=aes.encrypt(key, data);
byte digest[16];
hash.getMD5_128(data, digest, sizeof(digest));
std::ofstream file;
file.open(this->filename, std::ios::out | std::ios::binary);
file.write((char *) digest,sizeof(digest));
file.write(dataEncrypted.c_str(), dataEncrypted.size());
file.close();
this->data=data;
}
void FileManIOFile::write(std::string data){
AESCrypt aes;
HASHCrypt hash;
std::string dataEncrypted;
dataEncrypted=aes.encrypt(this->key, data);
byte digest[16];
hash.getMD5_128(data, digest, sizeof(digest));
std::ofstream file;
file.open(this->filename, std::ios::out | std::ios::binary);
file.write((char *) digest,sizeof(digest));
file.write(dataEncrypted.c_str(), dataEncrypted.size());
file.close();
this->data=data;
}
std::string FileManIOFile::getData(){
return this->data;
}
|