summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2015-03-11 09:51:03 +0400
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2015-03-11 09:51:03 +0400
commit783197aaa13abbfb7757ecec912dfe9cb6e52c75 (patch)
tree399f42e41babbcf697f21c30f4cabb1f121e6c34
parent998745fe45c89570bd1d02e0e60199fc535da89b (diff)
Implement FileManIOFileClass, general bug correction.
-rw-r--r--CryptClass/AESCrypt.cpp11
-rw-r--r--CryptClass/AESCrypt.hpp10
-rw-r--r--CryptClass/AbstractSKA.hpp4
-rw-r--r--CryptClass/HASHCrypt.cpp11
-rw-r--r--CryptClass/HASHCrypt.hpp5
-rw-r--r--IOFileClass/FileManIOFile.cpp98
-rw-r--r--IOFileClass/FileManIOFile.hpp90
-rw-r--r--main.cpp17
8 files changed, 228 insertions, 18 deletions
diff --git a/CryptClass/AESCrypt.cpp b/CryptClass/AESCrypt.cpp
index 08f00f2..0222e9a 100644
--- a/CryptClass/AESCrypt.cpp
+++ b/CryptClass/AESCrypt.cpp
@@ -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
)
);
}
diff --git a/CryptClass/AESCrypt.hpp b/CryptClass/AESCrypt.hpp
index a8fa976..4a0845c 100644
--- a/CryptClass/AESCrypt.hpp
+++ b/CryptClass/AESCrypt.hpp
@@ -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
diff --git a/CryptClass/AbstractSKA.hpp b/CryptClass/AbstractSKA.hpp
index de03b1b..ca7230b 100644
--- a/CryptClass/AbstractSKA.hpp
+++ b/CryptClass/AbstractSKA.hpp
@@ -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
diff --git a/CryptClass/HASHCrypt.cpp b/CryptClass/HASHCrypt.cpp
index 11fc04c..cd0dab6 100644
--- a/CryptClass/HASHCrypt.cpp
+++ b/CryptClass/HASHCrypt.cpp
@@ -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){
diff --git a/CryptClass/HASHCrypt.hpp b/CryptClass/HASHCrypt.hpp
index b377fac..2c17d3d 100644
--- a/CryptClass/HASHCrypt.hpp
+++ b/CryptClass/HASHCrypt.hpp
@@ -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
diff --git a/IOFileClass/FileManIOFile.cpp b/IOFileClass/FileManIOFile.cpp
index e69de29..d0ae1f2 100644
--- a/IOFileClass/FileManIOFile.cpp
+++ b/IOFileClass/FileManIOFile.cpp
@@ -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;
+}
diff --git a/IOFileClass/FileManIOFile.hpp b/IOFileClass/FileManIOFile.hpp
index e69de29..8a102c7 100644
--- a/IOFileClass/FileManIOFile.hpp
+++ b/IOFileClass/FileManIOFile.hpp
@@ -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
+
+
+
+
+
+};
diff --git a/main.cpp b/main.cpp
index 7a218d0..6b1bf23 100644
--- a/main.cpp
+++ b/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;