Make untracked folder
This commit is contained in:
parent
7f5576ded6
commit
77affb6d55
189 changed files with 10198 additions and 0 deletions
110
Untracked/Doxygen/CryptClass/AESCrypt.cpp
Normal file
110
Untracked/Doxygen/CryptClass/AESCrypt.cpp
Normal file
|
@ -0,0 +1,110 @@
|
|||
/**
|
||||
* @file AESCrypt.cpp
|
||||
* @brief AESCrypt class definitions
|
||||
* @author manzerbredes
|
||||
* @date 8 Mars 2015
|
||||
*
|
||||
* Contain all definitions of AESCrypt class.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "AESCrypt.hpp"
|
||||
|
||||
//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
|
||||
int pad=0;
|
||||
int tmpL=data.length();
|
||||
while(tmpL % 128 != 0){
|
||||
tmpL++;
|
||||
pad++;
|
||||
|
||||
}
|
||||
std::cout <<pad;
|
||||
while(data.length() % 128 != 0){
|
||||
data+=pad;
|
||||
}
|
||||
|
||||
|
||||
//Contain data encrypted
|
||||
std::string cipher;
|
||||
|
||||
//Use try, catch to be ensure no problems happening
|
||||
try{
|
||||
//Create encoder to encrypt data
|
||||
CryptoPP::ECB_Mode<CryptoPP::AES>::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::PKCS_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::PKCS_PADDING
|
||||
)
|
||||
);
|
||||
}
|
||||
catch( CryptoPP::Exception& e )
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//return decrypted data
|
||||
return cipher;
|
||||
}
|
71
Untracked/Doxygen/CryptClass/AESCrypt.hpp
Normal file
71
Untracked/Doxygen/CryptClass/AESCrypt.hpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* @file AESCrypt.hpp
|
||||
* @brief AESCrypt class header
|
||||
* @author manzerbredes
|
||||
* @date 8 Mars 2015
|
||||
*
|
||||
* Contain all prototypes of AESCrypt class.
|
||||
*
|
||||
*/
|
||||
|
||||
//----- std -----
|
||||
#include "AbstractSKA.hpp"
|
||||
#include "HASHCrypt.hpp"
|
||||
#include <iostream>
|
||||
|
||||
//----- crypto++ -----
|
||||
#include <crypto++/aes.h>
|
||||
#include <crypto++/modes.h>
|
||||
#include <crypto++/filters.h>
|
||||
|
||||
|
||||
/**
|
||||
* @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.
|
||||
|
||||
|
||||
};
|
54
Untracked/Doxygen/CryptClass/AbstractSKA.hpp
Normal file
54
Untracked/Doxygen/CryptClass/AbstractSKA.hpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* @file AbstractSKA.hpp
|
||||
* @brief Class for Symmetric-Key Algorithm (SKA)
|
||||
* @author manzerbredes
|
||||
* @date 8 Mars 2015
|
||||
*
|
||||
* Specify which method the algorithm must be implement.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
/**
|
||||
* @class AbstractSKA AbstractSKA.hpp "/CryptClass/AbstractSKA.hpp"
|
||||
* @brief Class for Symmetric-Key Algorithm (SKA)
|
||||
* @author manzerbredes
|
||||
*
|
||||
* This class should not be instanciate directly.
|
||||
*
|
||||
*/
|
||||
|
||||
class AbstractSKA {
|
||||
|
||||
public:
|
||||
AbstractSKA(){
|
||||
}
|
||||
~AbstractSKA(){
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encrypt data.
|
||||
*
|
||||
* @param key : key used to encrypt data
|
||||
* @param data : contain data to encrypt.
|
||||
*
|
||||
* This method must be overwritten.
|
||||
* **Warning** data will be modified.
|
||||
*
|
||||
*/
|
||||
virtual std::string encrypt(std::string key, std::string data) = 0;
|
||||
|
||||
/**
|
||||
* @brief Decrypt data.
|
||||
*
|
||||
* @param key : key used to decrypt data
|
||||
* @param data : contain data to decrypt.
|
||||
*
|
||||
* This method must be overwritten.
|
||||
* **Warning** data will be modified.
|
||||
*
|
||||
*/
|
||||
virtual std::string decrypt(std::string key, std::string data) = 0;
|
||||
};
|
70
Untracked/Doxygen/CryptClass/HASHCrypt.cpp
Normal file
70
Untracked/Doxygen/CryptClass/HASHCrypt.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
/**
|
||||
* @file HASHCrypt.cpp
|
||||
* @brief HASHCrypt class definitions
|
||||
* @author manzerbredes
|
||||
* @date 8 Mars 2015
|
||||
*
|
||||
* Contain all definitions of HASHCrypt class.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "HASHCrypt.hpp"
|
||||
|
||||
|
||||
//Constructor
|
||||
HASHCrypt::HASHCrypt(){
|
||||
}
|
||||
|
||||
//Destructor
|
||||
HASHCrypt::~HASHCrypt(){
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HASHCrypt::getMD5_128(std::string chain, byte* digest, int size){
|
||||
|
||||
//Digest size controller
|
||||
this->checkDigestSize(CryptoPP::Weak1::MD5::DIGESTSIZE,size);
|
||||
|
||||
//Create the MD5 on digest parameter
|
||||
CryptoPP::Weak1::MD5 hash;
|
||||
hash.CalculateDigest( digest, (byte*) chain.c_str(), chain.length() );
|
||||
|
||||
}
|
||||
|
||||
|
||||
void HASHCrypt::getSHA_256(std::string chain, byte* digest, int size){
|
||||
|
||||
//Digest size controller
|
||||
this->checkDigestSize(CryptoPP::SHA256::DIGESTSIZE,size);
|
||||
|
||||
//Create the SHA-256 on digest parameter
|
||||
CryptoPP::SHA256 hash;
|
||||
hash.CalculateDigest( digest, (byte*) chain.c_str(), chain.length() );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HASHCrypt::checkDigestSize(int sizeRequired, int size){
|
||||
try{
|
||||
if(size !=sizeRequired){
|
||||
throw this->getInvalidDigestSizeError(sizeRequired, size);
|
||||
}
|
||||
|
||||
}
|
||||
catch(std::string erreur){
|
||||
std::cerr << erreur <<std::endl;
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string HASHCrypt::getInvalidDigestSizeError(int sizeRequired, int size){
|
||||
std::ostringstream erreurStream;
|
||||
erreurStream << "Invalid digest size ! ("<< sizeRequired <<" bytes required and "<< size <<" given)";
|
||||
return erreurStream.str();
|
||||
}
|
||||
|
99
Untracked/Doxygen/CryptClass/HASHCrypt.hpp
Normal file
99
Untracked/Doxygen/CryptClass/HASHCrypt.hpp
Normal file
|
@ -0,0 +1,99 @@
|
|||
/**
|
||||
* @file HASHCrypt.hpp
|
||||
* @brief HASHCrypt class header
|
||||
* @author manzerbredes
|
||||
* @date 8 Mars 2015
|
||||
*
|
||||
* Contain all prototypes of HASHCrypt class.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
//----- std -----
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
//----- crypto++ -----
|
||||
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
|
||||
#include <crypto++/md5.h> //For MD5
|
||||
#include <crypto++/hex.h> //For Hex convertion
|
||||
#include <crypto++/sha.h> //For SHA
|
||||
#include <crypto++/modes.h>
|
||||
|
||||
|
||||
/**
|
||||
* @class HASHCrypt HASHCrypt.hpp "/CryptClass/HASHCrypt.hpp"
|
||||
* @brief Hashing class
|
||||
* @author manzerbredes
|
||||
*
|
||||
* Class who handle hashing functions to a byte* parameter.
|
||||
* HASHCrypt try to detect errors and throw exceptions.
|
||||
* HASHCrypt use crypto++ library.
|
||||
*/
|
||||
class HASHCrypt{
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Contructor
|
||||
*/
|
||||
HASHCrypt();
|
||||
|
||||
/**
|
||||
* @brief Destructor
|
||||
*/
|
||||
~HASHCrypt();
|
||||
|
||||
/**
|
||||
* @brief Create an MD5 over 128 bits on a digest array of bytes.
|
||||
*
|
||||
* @param chain : Chain to hash
|
||||
* @param digest : An array of bytes (8 bits)
|
||||
* @param size : Length of the array digest
|
||||
*
|
||||
* **Warning** digest will be modified.
|
||||
* Digest must be an array of byte with 16 entries
|
||||
*/
|
||||
void getMD5_128(std::string chain, byte* digest, int size);
|
||||
|
||||
/**
|
||||
* @brief Create an SHA over 256 bits on a digest array of bytes.
|
||||
*
|
||||
* @param chain : Chain to hash
|
||||
* @param digest : An array of bytes (8 bits)
|
||||
* @param size : Length of the array digest
|
||||
*
|
||||
* **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
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* @brief Check the digest size
|
||||
*
|
||||
* @param sizeRequired : Digest size expected
|
||||
* @param size : Given digest size
|
||||
*
|
||||
* Throw an exception, and stop the programm if
|
||||
* sizeRequired != size
|
||||
* Use getInvalidDigestSizeError method.
|
||||
*/
|
||||
void checkDigestSize(int sizeRequired, int size);
|
||||
|
||||
/**
|
||||
* @brief Make and error message.
|
||||
*
|
||||
* @param sizeRequired : Digest size expected
|
||||
* @param size : Given digest size
|
||||
*
|
||||
* @return a string correspond to the error message
|
||||
*
|
||||
* Construct an error message with sizeRequired and size.
|
||||
*/
|
||||
std::string getInvalidDigestSizeError(int sizeRequired, int size);
|
||||
|
||||
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue