Add support to save without key after read file with the key.

This commit is contained in:
manzerbredes 2015-03-11 17:04:01 +04:00
parent e261782473
commit 5783bf5e10
3 changed files with 48 additions and 11 deletions

View file

@ -16,7 +16,7 @@ FileManIOFile::FileManIOFile(std::string filename){
this->filename=filename; this->filename=filename;
this->readable=false; this->readable=false;
this->data=""; this->data="";
this->key[0]=NULL; this->key;
} }
FileManIOFile::~FileManIOFile(){ FileManIOFile::~FileManIOFile(){
} }
@ -68,18 +68,18 @@ void FileManIOFile::read(std::string key){
} }
void FileManIOFile::write(std::string key, std::string data){
void FileManIOFile::write(std::string key,std::string data){
AESCrypt aes; AESCrypt aes;
HASHCrypt hash; HASHCrypt hash;
std::string dataEncrypted; std::string dataEncrypted;
if(this->key!=NULL){ dataEncrypted=aes.encrypt(key, data);
dataEncrypted=aes.encrypt(key, data);
}
else{
dataEncrypted=aes.encrypt(this->key, data);
}
byte digest[16]; byte digest[16];
hash.getMD5_128(data, digest, sizeof(digest)); hash.getMD5_128(data, digest, sizeof(digest));
@ -100,6 +100,34 @@ void FileManIOFile::write(std::string key, std::string 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(){ std::string FileManIOFile::getData(){
return this->data; return this->data;

View file

@ -58,7 +58,9 @@ class FileManIOFile {
* Save data to "filename" attribute. * Save data to "filename" attribute.
* *
*/ */
void write(std::string data,std::string key=NULL); void write(std::string key, std::string data);
void write(std::string data);
/** /**
* @brief True if file fully decrypted. * @brief True if file fully decrypted.

View file

@ -38,9 +38,16 @@ int main(int argc, char *argv[]){
AESCrypt aes; AESCrypt aes;
FileManIOFile fichier = FileManIOFile("Doxygen/bob2.bin"); FileManIOFile fichier = FileManIOFile("Doxygen/bob.bin");
fichier.write(key,chaine); fichier.write(key, chaine);
fichier.read(key);
if(fichier.isReadable())
std::cout << fichier.getData();
fichier.write(chaine+" YES");
fichier.read(key); fichier.read(key);