AES first release. Add method to HASHCrypt class.

This commit is contained in:
manzerbredes 2015-03-09 12:51:23 +04:00
parent 1574c191c3
commit 7d2c473422
6 changed files with 263 additions and 32 deletions

View file

@ -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 <iomanip>
//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<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::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<cipher.length();i++){
if(cipher[i]=='\0')
break;
}
cipher.erase(i,cipher.length()-1);
//return decrypted data
return cipher;
}

View 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.
};

View file

@ -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;
};

View file

@ -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<size; i++){
//Return false if digest are different
if(digest1[i] != digest2[i]){
return false;
}
}
}
catch (std::exception& e)
{
std::cerr << "Exception catched : " << e.what() << std::endl;
}
//Return true if digest are equals
return true;
}
//Convert digest to string
std::string HASHCrypt::digestToString(byte* digest, int size){
CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach( new CryptoPP::StringSink( output ) );
encoder.Put( digest, size );
encoder.MessageEnd();
return output;
}

View file

@ -8,6 +8,8 @@
*
*/
#ifndef __HASHCrypt__
#define __HASHCrypt__
//----- std -----
#include <iostream>
@ -15,11 +17,11 @@
#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>
/**
@ -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

View file

@ -17,13 +17,10 @@
#include <string>
//----- 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;
}