diff --git a/CryptClass/AESCrypt.cpp b/CryptClass/AESCrypt.cpp index e69de29..78a553e 100644 --- a/CryptClass/AESCrypt.cpp +++ b/CryptClass/AESCrypt.cpp @@ -0,0 +1,117 @@ +/** + * @file AESCrypt.cpp + * @brief AESCrypt class definitions + * @author manzerbredes + * @date 8 Mars 2015 + * + * Contain all definitions of AESCrypt class. + * + */ + +#include "AESCrypt.hpp" +#include +//Constructor +AESCrypt::AESCrypt(){ + this->hash=HASHCrypt(); //Init hash attribute +} + +//Destructor +AESCrypt::~AESCrypt(){ +} + + +//Encrypt string +std::string AESCrypt::encrypt(std::string key, std::string data){ + + //Generate SHA-256 + byte digest[32]; + hash.getSHA_256(key, digest, (int)sizeof(digest)); + + + + + //Add padding for AES + /*char pad=0x01; + int tmpL=data.length(); + while(tmpL % 128 != 0){ + tmpL++; + pad+=1; + + } + std::cout << "pad:"<< std::hex << pad; + while(data.length() % 128 != 0){ + data+=pad; + } + + std::cout << data.length();*/ + //Contain data encrypted + std::string cipher; + + //Use try, catch to be ensure no problems happening + try{ + //Create encoder to encrypt data + CryptoPP::ECB_Mode::Encryption encoder; + encoder.SetKey( digest, sizeof(digest) ); + + //Encrypt data with StreamTransformationFilter with NO PADDING + CryptoPP::StringSource ss1(data, true, + new CryptoPP::StreamTransformationFilter( encoder, + new CryptoPP::StringSink( cipher ), + CryptoPP::StreamTransformationFilter::ZEROS_PADDING + + ) + ); + } + catch( CryptoPP::Exception& e ) + { + std::cerr << e.what() << std::endl; + exit(EXIT_FAILURE); + } + + //return encrypted data + return cipher; + +} + +//Decrypt string +std::string AESCrypt::decrypt(std::string key, std::string data){ + + + byte digest[32]; + hash.getSHA_256(key, digest, (int)sizeof(digest)); + + + //Contain data decrypted + std::string cipher; + + //Use try, catch to be ensure no problems happening + try { + + //Create decoder to encrypt data + CryptoPP::ECB_Mode< CryptoPP::AES >::Decryption decoder; + decoder.SetKey( digest, sizeof(digest) ); + + //Decrypt data with StreamTransformationFilter with NO PADDING + CryptoPP::StringSource ss3( data, true, + new CryptoPP::StreamTransformationFilter( decoder, + new CryptoPP::StringSink( cipher ), + CryptoPP::StreamTransformationFilter::ZEROS_PADDING + ) + ); + } + catch( CryptoPP::Exception& e ) + { + std::cerr << e.what() << std::endl; + exit(1); + } + + + int i=0; + for(i=0;i + +//----- crypto++ ----- +#include +#include +#include + + +/** + * @class AESCrypt AESCrypt.hpp "/CryptClass/AESCrypt.hpp" + * @brief Class for Advanced Encryption Standard (AES) algorithm + * @author manzerbredes + * + * This class provide AES encrypt and decrypt. + * + */ + +class AESCrypt : public AbstractSKA { + + + public: + AESCrypt(); + ~AESCrypt(); + + + /** + * @brief Encrypt data with AES algorithm. + * + * @param key : key to encrypt data + * @param data : contain data to encrypt. + * + * @return string : correspond to crypted data + * + * Encrypt data, and return them in a string. + * Padding are blank space. + * + */ + virtual std::string encrypt(std::string key, std::string data); + + /** + * @brief Decrypt data from AES algorithm. + * + * @param key : key used to encrypt data + * @param data : contain data to decrypt from AES encrypt. + * + * @return string : correspond to decrypted data + * + * Decrypt data, and return them in a string. + * Padding is not removed. + * + */ + virtual std::string decrypt(std::string key, std::string data); + + private: + HASHCrypt hash; ///< hash instance to generate SHA-256 hash code. + + +}; diff --git a/CryptClass/AbstractSKA.hpp b/CryptClass/AbstractSKA.hpp index 57a4a23..de03b1b 100644 --- a/CryptClass/AbstractSKA.hpp +++ b/CryptClass/AbstractSKA.hpp @@ -38,7 +38,7 @@ class AbstractSKA { * **Warning** data will be modified. * */ - virtual void encrypt(std::string key, char* data) = 0; + virtual std::string encrypt(std::string key, std::string data) = 0; /** * @brief Decrypt data. @@ -50,5 +50,5 @@ class AbstractSKA { * **Warning** data will be modified. * */ - virtual void decrypt(std::string key, char* data) = 0; + virtual std::string decrypt(std::string key, std::string data) = 0; }; diff --git a/CryptClass/HASHCrypt.cpp b/CryptClass/HASHCrypt.cpp index 4c42ef3..11fc04c 100644 --- a/CryptClass/HASHCrypt.cpp +++ b/CryptClass/HASHCrypt.cpp @@ -42,6 +42,8 @@ void HASHCrypt::getSHA_256(std::string chain, byte* digest, int size){ //Create the SHA-256 on digest parameter CryptoPP::SHA256 hash; hash.CalculateDigest( digest, (byte*) chain.c_str(), chain.length() ); + + } @@ -66,3 +68,37 @@ std::string HASHCrypt::getInvalidDigestSizeError(int sizeRequired, int size){ return erreurStream.str(); } + +bool HASHCrypt::compareDigest(byte* digest1, byte* digest2, int size){ + + //Try is more safe + try + { + //Compare the two digest + for(int i=0; i @@ -15,11 +17,11 @@ #include //----- crypto++ ----- +#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 #include //For MD5 #include //For Hex convertion #include //For SHA - - +#include /** @@ -66,7 +68,35 @@ class HASHCrypt{ * **Warning** digest will be modified. * Digest must be an array of byte with 32 entries */ - void getSHA_256(std::string chain, byte* digest, int size); //Retourne SHA_256 + void getSHA_256(std::string chain, byte* digest, int size); //Return SHA_256 + + + /** + * @brief Convert digest to a string of HEX characters + * + * @param digest : An array of bytes (8 bits) + * @param size : Length of the array digest + * + * @return a string of hex digest equivalent + * + * Digest must be an array of byte with 16 entries + */ + std::string digestToString(byte* digest, int size); //Return a string of a digest + + /** + * @brief Compare 2 digest + * + * @param digest1 : An array of bytes (8 bits) + * @param digest2 : An array of bytes (8 bits) + * @param size : Length of the array digest1 or digest2 + * + * @return a boolean if digest1 equals to digest2 + * + * **Warning** if sizeof(digest1) != sizeof(digest 2) : segmentation fault ! + * Compare the two digest. + */ + bool compareDigest(byte* digest1, byte* digest2, int size); + private: @@ -97,3 +127,5 @@ class HASHCrypt{ }; + +#endif diff --git a/main.cpp b/main.cpp index 45f62de..b65d5ba 100644 --- a/main.cpp +++ b/main.cpp @@ -17,13 +17,10 @@ #include //----- class ----- +#include "CryptClass/AESCrypt.hpp" #include "CryptClass/HASHCrypt.hpp" -//----- Prototype ----- -void aff(std::string chaine); - - /** @@ -37,30 +34,8 @@ void aff(std::string chaine); int main(int argc, char *argv[]){ - - - - - HASHCrypt hash= HASHCrypt(); - - byte code[16]; - - hash.getMD5_128("Phrase de test !", code, sizeof(code)); - - for(int i=0; i<16;i++){ - std::cout << code[i]; - } - + std::cout << "It's work !" << std::endl; return 0; } - - - - -//---- Functions ----- - -void aff(std::string chaine){ - std::cout << chaine; -}