summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CryptClass/AESCrypt.cpp17
-rw-r--r--CryptClass/AESCrypt.hpp23
-rw-r--r--IOFileClass/FileManIOFile.cpp35
-rw-r--r--IOFileClass/FileManIOFile.hpp26
-rw-r--r--main.cpp11
5 files changed, 101 insertions, 11 deletions
diff --git a/CryptClass/AESCrypt.cpp b/CryptClass/AESCrypt.cpp
index 30e501a..4f7309e 100644
--- a/CryptClass/AESCrypt.cpp
+++ b/CryptClass/AESCrypt.cpp
@@ -27,13 +27,24 @@ AESCrypt::~AESCrypt(){
-//Encrypt string
+//Encrypt string with string key
std::string AESCrypt::encrypt(std::string key, std::string data){
//Generate SHA-256
byte digest[32];
hash.getSHA_256(key, digest, (int)sizeof(digest));
+ return encryptRoutine(data, digest, sizeof(digest));
+
+}
+
+//Encrypt string with byte* key
+std::string AESCrypt::encrypt(byte* key, std::string data){
+ return encryptRoutine(data, key, 32);
+}
+
+//The encryptRoutine
+std::string AESCrypt::encryptRoutine(std::string data, byte* digest, int size){
//Contain data encrypted
std::string cipher;
@@ -42,7 +53,7 @@ std::string AESCrypt::encrypt(std::string key, std::string data){
try{
//Create encoder to encrypt data
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption encoder;
- encoder.SetKey( digest, sizeof(digest) );
+ encoder.SetKey( digest, size );
//Encrypt data with StreamTransformationFilter with NO PADDING
CryptoPP::StringSource ss1(data, true,
@@ -62,10 +73,12 @@ std::string AESCrypt::encrypt(std::string key, std::string data){
//return encrypted data
return cipher;
+
}
+
//Decrypt string
std::string AESCrypt::decrypt(std::string key, std::string data){
diff --git a/CryptClass/AESCrypt.hpp b/CryptClass/AESCrypt.hpp
index 455eed0..5e16672 100644
--- a/CryptClass/AESCrypt.hpp
+++ b/CryptClass/AESCrypt.hpp
@@ -41,6 +41,21 @@ class AESCrypt : public AbstractSKA {
/**
+ * @brief Run encryptRoutine.
+ *
+ * @param key : key used to encrypt data
+ * @param data : contain data to encrypt.
+ *
+ * @return string : correspond to crypted data
+ *
+ * Run encryptRoutine with byte* key or string key
+ *
+ */
+ std::string encrypt(std::string key, std::string data);
+ std::string encrypt(byte* key, std::string data);
+
+
+ /**
* @brief Encrypt data with AES algorithm.
*
* @param key : key used to encrypt data
@@ -48,11 +63,13 @@ class AESCrypt : public AbstractSKA {
*
* @return string : correspond to crypted data
*
- * Encrypt data, and return them in a string.
+ * Encrypt data, and return them into a string.
* Padding are blank space.
*
*/
- virtual std::string encrypt(std::string key, std::string data);
+ std::string encryptRoutine(std::string data, byte* digest, int size);
+
+
/**
* @brief Decrypt data from AES algorithm.
@@ -67,6 +84,8 @@ class AESCrypt : public AbstractSKA {
*/
virtual std::string decrypt(std::string key, std::string data);
+
+
private:
HASHCrypt hash; ///< hash instance to generate SHA-256 hash code.
diff --git a/IOFileClass/FileManIOFile.cpp b/IOFileClass/FileManIOFile.cpp
index d0ae1f2..bee2ebe 100644
--- a/IOFileClass/FileManIOFile.cpp
+++ b/IOFileClass/FileManIOFile.cpp
@@ -16,6 +16,7 @@ FileManIOFile::FileManIOFile(std::string filename){
this->filename=filename;
this->readable=false;
this->data="";
+ this->key;
}
FileManIOFile::~FileManIOFile(){
}
@@ -55,6 +56,7 @@ void FileManIOFile::read(std::string key){
if(hash.compareDigest(fileMD5, currentMD5, sizeof(currentMD5))){
this->readable=true;
+ hash.getSHA_256(key, this->key, 32);
}
else{
this->readable=false;
@@ -66,12 +68,39 @@ 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;
+ std::string dataEncrypted;
+
+ dataEncrypted=aes.encrypt(key, data);
+
+ this->writeRoutine(data, dataEncrypted);
+
+
+}
+
+void FileManIOFile::write(std::string data){
+ if(not(this->readable)){
+ std::cout << "Can't write data without key (read it before) !" << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+ AESCrypt aes;
+ std::string dataEncrypted;
+
+ dataEncrypted=aes.encrypt(this->key, data);
+ this->writeRoutine(data, dataEncrypted);
+
+
+}
+
+
+void FileManIOFile::writeRoutine(std::string data, std::string dataEncrypted){
HASHCrypt hash;
- std::string dataEncrypted=aes.encrypt(key, data);
byte digest[16];
hash.getMD5_128(data, digest, sizeof(digest));
@@ -89,10 +118,10 @@ void FileManIOFile::write(std::string key, std::string data){
file.close();
this->data=data;
-
}
+
std::string FileManIOFile::getData(){
return this->data;
}
diff --git a/IOFileClass/FileManIOFile.hpp b/IOFileClass/FileManIOFile.hpp
index 4dfcfc4..7013b27 100644
--- a/IOFileClass/FileManIOFile.hpp
+++ b/IOFileClass/FileManIOFile.hpp
@@ -50,15 +50,35 @@ class FileManIOFile {
*/
void read(std::string key);
+
/**
- * @brief Read encrypted file.
+ * @brief Write data in encrypted file.
*
* @param key : key to encrypt data
+ * @param data : data to write
*
- * Save data to "filename" attribute.
+ * Write the file with or without key
+ * To write data without key, you need to read it before (to save the key
+ * in attribute key;
*
*/
void write(std::string key, std::string data);
+ void write(std::string data);
+
+
+ /**
+ * @brief Write data in encrypted file.
+ *
+ * @param data : data to write (for MD5)
+ * @param dataEncrypted : data to write
+ *
+ * Write encryptedData to filename
+ *
+ */
+ void writeRoutine(std::string data, std::string dataEncrypted);
+
+
+
/**
* @brief True if file fully decrypted.
@@ -87,6 +107,8 @@ class FileManIOFile {
bool readable; ///< Readable attribute
+ byte key[32]; ///< Key in SHA-256
+
diff --git a/main.cpp b/main.cpp
index dd455a5..bae168f 100644
--- a/main.cpp
+++ b/main.cpp
@@ -61,9 +61,16 @@ int main(int argc, char *argv[]){
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);