Implement FileManIOFileClass, general bug correction.
This commit is contained in:
parent
998745fe45
commit
783197aaa1
8 changed files with 228 additions and 18 deletions
|
@ -8,8 +8,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
//----- class -----
|
||||
#include "AESCrypt.hpp"
|
||||
|
||||
|
||||
|
||||
|
||||
//Constructor
|
||||
AESCrypt::AESCrypt(){
|
||||
this->hash=HASHCrypt(); //Init hash attribute
|
||||
|
@ -20,6 +24,8 @@ AESCrypt::~AESCrypt(){
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Encrypt string
|
||||
std::string AESCrypt::encrypt(std::string key, std::string data){
|
||||
|
||||
|
@ -30,6 +36,7 @@ std::string AESCrypt::encrypt(std::string key, std::string data){
|
|||
//Contain data encrypted
|
||||
std::string cipher;
|
||||
|
||||
|
||||
//Use try, catch to be ensure no problems happening
|
||||
try{
|
||||
//Create encoder to encrypt data
|
||||
|
@ -56,6 +63,8 @@ std::string AESCrypt::encrypt(std::string key, std::string data){
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Decrypt string
|
||||
std::string AESCrypt::decrypt(std::string key, std::string data){
|
||||
|
||||
|
@ -78,7 +87,7 @@ std::string AESCrypt::decrypt(std::string key, std::string data){
|
|||
CryptoPP::StringSource ss3( data, true,
|
||||
new CryptoPP::StreamTransformationFilter( decoder,
|
||||
new CryptoPP::StringSink( cipher ),
|
||||
CryptoPP::StreamTransformationFilter::ZEROS_PADDING
|
||||
CryptoPP::StreamTransformationFilter::NO_PADDING
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,9 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#ifndef __AESCrypt__
|
||||
#define __AESCrypt__
|
||||
|
||||
//----- std -----
|
||||
#include "AbstractSKA.hpp"
|
||||
#include "HASHCrypt.hpp"
|
||||
|
@ -39,7 +42,7 @@ class AESCrypt : public AbstractSKA {
|
|||
/**
|
||||
* @brief Encrypt data with AES algorithm.
|
||||
*
|
||||
* @param key : key to encrypt data
|
||||
* @param key : key used to encrypt data
|
||||
* @param data : contain data to encrypt.
|
||||
*
|
||||
* @return string : correspond to crypted data
|
||||
|
@ -58,8 +61,7 @@ class AESCrypt : public AbstractSKA {
|
|||
*
|
||||
* @return string : correspond to decrypted data
|
||||
*
|
||||
* Decrypt data, and return them in a string.
|
||||
* Padding is not removed.
|
||||
* Decrypt data, and return them into a string.
|
||||
*
|
||||
*/
|
||||
virtual std::string decrypt(std::string key, std::string data);
|
||||
|
@ -69,3 +71,5 @@ class AESCrypt : public AbstractSKA {
|
|||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
* Specify which method the algorithm must be implement.
|
||||
*
|
||||
*/
|
||||
#ifndef __AbstractSKA__
|
||||
#define __AbstractSKA__
|
||||
|
||||
#include <string>
|
||||
|
||||
|
@ -52,3 +54,5 @@ class AbstractSKA {
|
|||
*/
|
||||
virtual std::string decrypt(std::string key, std::string data) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -8,10 +8,13 @@
|
|||
*
|
||||
*/
|
||||
|
||||
|
||||
//----- class -----
|
||||
#include "HASHCrypt.hpp"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//Constructor
|
||||
HASHCrypt::HASHCrypt(){
|
||||
}
|
||||
|
@ -47,7 +50,7 @@ void HASHCrypt::getSHA_256(std::string chain, byte* digest, int size){
|
|||
}
|
||||
|
||||
|
||||
|
||||
//Check the size of the digest
|
||||
void HASHCrypt::checkDigestSize(int sizeRequired, int size){
|
||||
try{
|
||||
if(size !=sizeRequired){
|
||||
|
@ -61,7 +64,7 @@ void HASHCrypt::checkDigestSize(int sizeRequired, int size){
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//Make the error
|
||||
std::string HASHCrypt::getInvalidDigestSizeError(int sizeRequired, int size){
|
||||
std::ostringstream erreurStream;
|
||||
erreurStream << "Invalid digest size ! ("<< sizeRequired <<" bytes required and "<< size <<" given)";
|
||||
|
@ -69,6 +72,7 @@ std::string HASHCrypt::getInvalidDigestSizeError(int sizeRequired, int size){
|
|||
}
|
||||
|
||||
|
||||
//Compare 2 digest (same size)
|
||||
bool HASHCrypt::compareDigest(byte* digest1, byte* digest2, int size){
|
||||
|
||||
//Try is more safe
|
||||
|
@ -91,6 +95,7 @@ bool HASHCrypt::compareDigest(byte* digest1, byte* digest2, int size){
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
//Convert digest to string
|
||||
std::string HASHCrypt::digestToString(byte* digest, int size){
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
* @brief Hashing class
|
||||
* @author manzerbredes
|
||||
*
|
||||
* Class who handle hashing functions to a byte* parameter.
|
||||
* Class who handle hashing functions on a byte* parameter.
|
||||
* HASHCrypt try to detect errors and throw exceptions.
|
||||
* HASHCrypt use crypto++ library.
|
||||
*/
|
||||
|
@ -111,6 +111,7 @@ class HASHCrypt{
|
|||
*/
|
||||
void checkDigestSize(int sizeRequired, int size);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Make and error message.
|
||||
*
|
||||
|
@ -123,8 +124,6 @@ class HASHCrypt{
|
|||
*/
|
||||
std::string getInvalidDigestSizeError(int sizeRequired, int size);
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
/**
|
||||
* @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="";
|
||||
}
|
||||
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;
|
||||
}
|
||||
else{
|
||||
this->readable=false;
|
||||
}
|
||||
|
||||
|
||||
file.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void FileManIOFile::write(std::string key, std::string data){
|
||||
|
||||
AESCrypt aes;
|
||||
HASHCrypt hash;
|
||||
|
||||
std::string 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
std::string FileManIOFile::getData(){
|
||||
return this->data;
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/**
|
||||
* @file FileManIOFile.hpp
|
||||
* @brief FileManIOFile class definitions
|
||||
* @author manzerbredes
|
||||
* @date 9 Mars 2015
|
||||
*
|
||||
* Contain all definitions of FileManIOFile class.
|
||||
*
|
||||
*/
|
||||
|
||||
//--- std -----
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
//----- class -----
|
||||
#include "HASHCrypt.hpp"
|
||||
#include "AESCrypt.hpp"
|
||||
|
||||
|
||||
/**
|
||||
* @class FileManIOFile FileManIOFile.hpp "/CryptClass/FileManIOFile.hpp"
|
||||
* @brief Class for quick open and close encrypted file.
|
||||
* @author manzerbredes
|
||||
*
|
||||
* -----File organisation-----
|
||||
*
|
||||
* 16 first bytes : md5 of decrypted file
|
||||
* rest of the file : data encrypted (ASE for now)
|
||||
*
|
||||
*/
|
||||
class FileManIOFile {
|
||||
|
||||
public:
|
||||
FileManIOFile(std::string filename);
|
||||
~FileManIOFile();
|
||||
|
||||
/**
|
||||
* @brief Read encrypted file.
|
||||
*
|
||||
* @param key : key to encrypt data
|
||||
*
|
||||
* Read data from "filename" attribute.
|
||||
* If file fully decrypted, readable var switch to true.
|
||||
*
|
||||
*/
|
||||
void read(std::string key);
|
||||
|
||||
/**
|
||||
* @brief Read encrypted file.
|
||||
*
|
||||
* @param key : key to encrypt data
|
||||
*
|
||||
* Save data to "filename" attribute.
|
||||
*
|
||||
*/
|
||||
void write(std::string key, std::string data);
|
||||
|
||||
/**
|
||||
* @brief True if file fully decrypted.
|
||||
*
|
||||
* Return "readable" attribute.
|
||||
*
|
||||
*/
|
||||
bool isReadable();
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get data attribute.
|
||||
*
|
||||
* Return "data" attribute.
|
||||
*
|
||||
* **Warning** if data not fully decrypted (readable!=true),
|
||||
* data will be unreadable.
|
||||
*/
|
||||
std::string getData();
|
||||
|
||||
private:
|
||||
|
||||
std::string filename; ///< Filename attribute
|
||||
|
||||
std::string data; ///< Data attribute
|
||||
|
||||
bool readable; ///< Readable attribute
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
17
main.cpp
17
main.cpp
|
@ -17,9 +17,9 @@
|
|||
#include <string>
|
||||
|
||||
//----- class -----
|
||||
#include "CryptClass/AESCrypt.hpp"
|
||||
#include "CryptClass/HASHCrypt.hpp"
|
||||
|
||||
#include "AESCrypt.hpp"
|
||||
#include "HASHCrypt.hpp"
|
||||
#include "FileManIOFile.hpp"
|
||||
|
||||
|
||||
|
||||
|
@ -34,17 +34,18 @@
|
|||
int main(int argc, char *argv[]){
|
||||
|
||||
std::string chaine="It's work !";
|
||||
|
||||
std::string key="loic";
|
||||
AESCrypt aes;
|
||||
|
||||
chaine=aes.encrypt("loic", chaine);
|
||||
|
||||
std::cout << chaine << std::endl;
|
||||
FileManIOFile fichier = FileManIOFile("Doxygen/bob2.bin");
|
||||
|
||||
chaine=aes.decrypt("loic", chaine);
|
||||
fichier.write(key,chaine);
|
||||
|
||||
std::cout << chaine << std::endl;
|
||||
fichier.read(key);
|
||||
|
||||
if(fichier.isReadable())
|
||||
std::cout << fichier.getData();
|
||||
|
||||
return 0;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue