summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2015-03-09 12:51:23 +0400
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2015-03-09 12:51:23 +0400
commit7d2c4734224eb7f6cf699bc84f505c06c4394b35 (patch)
tree070330590915595ec740acfeea5a92c742f427fb
parent1574c191c3a6909e2b932fb32d6c63dc89e264b1 (diff)
AES first release. Add method to HASHCrypt class.
-rw-r--r--CryptClass/AESCrypt.cpp117
-rw-r--r--CryptClass/AESCrypt.hpp71
-rw-r--r--CryptClass/AbstractSKA.hpp4
-rw-r--r--CryptClass/HASHCrypt.cpp36
-rw-r--r--CryptClass/HASHCrypt.hpp38
-rw-r--r--main.cpp29
6 files changed, 263 insertions, 32 deletions
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 <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;
+}
diff --git a/CryptClass/AESCrypt.hpp b/CryptClass/AESCrypt.hpp
index e69de29..a8fa976 100644
--- a/CryptClass/AESCrypt.hpp
+++ b/CryptClass/AESCrypt.hpp
@@ -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.
+
+
+};
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<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;
+}
diff --git a/CryptClass/HASHCrypt.hpp b/CryptClass/HASHCrypt.hpp
index 2436040..b4b02fd 100644
--- a/CryptClass/HASHCrypt.hpp
+++ b/CryptClass/HASHCrypt.hpp
@@ -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
diff --git a/main.cpp b/main.cpp
index 45f62de..b65d5ba 100644
--- a/main.cpp
+++ b/main.cpp
@@ -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;
-}