diff --git a/.idea/.name b/.idea/.name
new file mode 100644
index 0000000..e9ff9a0
--- /dev/null
+++ b/.idea/.name
@@ -0,0 +1 @@
+aes
\ No newline at end of file
diff --git a/.idea/aes.iml b/.idea/aes.iml
new file mode 100644
index 0000000..921849b
--- /dev/null
+++ b/.idea/aes.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..97626ba
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..3eb495b
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..1de4e38
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..391076c
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,320 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ C/C++
+
+
+
+
+ Unused codeC/C++
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1453148077173
+
+ 1453148077173
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..7232844
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,8 @@
+cmake_minimum_required(VERSION 3.3)
+project(aes)
+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -ggdb -lcrypto")
+include_directories("/usr/include/openssl/")
+
+set(SOURCE_FILES aes.cpp)
+add_executable(crypto.ex ${SOURCE_FILES})
\ No newline at end of file
diff --git a/aes.cpp b/aes.cpp
index 289f636..84f49d8 100644
--- a/aes.cpp
+++ b/aes.cpp
@@ -1,256 +1,83 @@
-#include
-#include
-#include
-#include
-#include
-
-#include
-#include
-#include
-#include
-
-#define FAILURE -1
-#define SUCCESS 0
-
-#define AES_KEYLEN 256
-#define AES_ROUNDS 6
-
-
-int aesDecrypt(EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV, unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg) {
- size_t decLen = 0;
- size_t blockLen = 0;
-
- *decMsg = (unsigned char*)malloc(encMsgLen);
- if(*decMsg == NULL) return FAILURE;
-
- if(!EVP_DecryptInit_ex(aesDecryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
- return FAILURE;
- }
-
- if(!EVP_DecryptUpdate(aesDecryptCtx, (unsigned char*)*decMsg, (int*)&blockLen, encMsg, (int)encMsgLen)) {
- return FAILURE;
- }
- decLen += blockLen;
-
- if(!EVP_DecryptFinal_ex(aesDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
- return FAILURE;
- }
- decLen += blockLen;
-
- EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
-
- return (int)decLen;
-}
-
-int aesEncrypt(EVP_CIPHER_CTX* aesEncryptCtx, unsigned char* aesKey, unsigned char* aesIV, const unsigned char *msg, size_t msgLen, unsigned char **encMsg) {
- size_t blockLen = 0;
- size_t encMsgLen = 0;
-
- *encMsg = (unsigned char*)malloc(msgLen + AES_BLOCK_SIZE);
- if(encMsg == NULL) return FAILURE;
-
- if(!EVP_EncryptInit_ex(aesEncryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
- return FAILURE;
- }
-
- if(!EVP_EncryptUpdate(aesEncryptCtx, *encMsg, (int*)&blockLen, (unsigned char*)msg, msgLen)) {
- return FAILURE;
- }
- encMsgLen += blockLen;
-
- if(!EVP_EncryptFinal_ex(aesEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen)) {
- return FAILURE;
- }
-
- EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
-
- return encMsgLen + blockLen;
-}
-
-void clear_all(EVP_CIPHER_CTX* aesEncryptCtx, EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV) {
- EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
- EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
-
- free(aesEncryptCtx);
- free(aesDecryptCtx);
-
- free(aesIV);
- free(aesKey);
-}
-
-void init_all(EVP_CIPHER_CTX* aesEncryptCtx, EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV) {
- EVP_CIPHER_CTX_init(aesEncryptCtx);
- EVP_CIPHER_CTX_init(aesDecryptCtx);
-
- unsigned char *aesPass = (unsigned char*)malloc(AES_KEYLEN/8);
- unsigned char *aesSalt = (unsigned char*)malloc(8);
-
- if(aesKey == NULL || aesIV == NULL || aesPass == NULL || aesSalt == NULL) {
- exit(FAILURE);
- }
-
- #define USE_PBKDF
- #ifdef USE_PBKDF
- std::cerr << "utilisation de USE_PBKDF" << std::endl;
- // Get some random data to use as the AES pass and salt
- if(RAND_bytes(aesPass, AES_KEYLEN/8) == 0) {
- exit(FAILURE);
- }
-
- if(RAND_bytes(aesSalt, 8) == 0) {
- exit(FAILURE);
- }
-
- if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, AES_KEYLEN/8, AES_ROUNDS, aesKey, aesIV) == 0) {
- exit(FAILURE);
- }
- #else
- if(RAND_bytes(aesKey, AES_KEYLEN/8) == 0) {
- exit(FAILURE);
- }
-
- if(RAND_bytes(aesIV, AES_KEYLEN/8) == 0) {
- exit(FAILURE);
- }
- #endif
-
- free(aesPass);
- free(aesSalt);
-}
-
-void writeFile(char *filename, unsigned char *file, size_t fileLength) {
- FILE *fd = fopen(filename, "wb");
- if(fd == NULL) {
- fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
- exit(1);
- }
-
- size_t bytesWritten = fwrite(file, 1, fileLength, fd);
-
- if(bytesWritten != fileLength) {
- fprintf(stderr, "Failed to write file\n");
- exit(1);
- }
-
- fclose(fd);
-}
-
-int readFile(char *filename, unsigned char **file) {
- FILE *fd = fopen(filename, "rb");
- if(fd == NULL) {
- fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
- exit(1);
- }
-
- // Determine size of the file
- fseek(fd, 0, SEEK_END);
- size_t fileLength = ftell(fd);
- fseek(fd, 0, SEEK_SET);
-
- // Allocate space for the file
- *file = (unsigned char*)malloc(fileLength);
- if(*file == NULL) {
- fprintf(stderr, "Failed to allocate memory\n");
- exit(1);
- }
-
- // Read the file into the buffer
- size_t bytesRead = fread(*file, 1, fileLength, fd);
-
- if(bytesRead != fileLength) {
- fprintf(stderr, "Error reading file\n");
- exit(1);
- }
-
- fclose(fd);
-
- return fileLength;
-}
+#include "aes.hpp"
+#include "crypt.hpp"
+#include "decrypt.hpp"
int main(int argc, char* argv[]) {
if(argc != 2) {
- fprintf(stderr, "No file argument supplied.\n");
+ std::cerr << "No file argument supplied.\n";
return 1;
}
- EVP_CIPHER_CTX* aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
- EVP_CIPHER_CTX* aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
- if(aesDecryptCtx == NULL || aesEncryptCtx == NULL) {
- return FAILURE;
- }
-
- unsigned char* aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
- unsigned char* aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
-
- init_all(aesEncryptCtx, aesDecryptCtx, aesKey, aesIV);
-
//***************************************************
char* filename = argv[1];
+ aes O = crypt(filename);
+
// Read the file to encrypt
unsigned char *file;
- size_t fileLength = readFile(filename, &file);
+ // readFile fait l'aloccation mémoire !!! pensé au free
+ size_t fileLength = O.readFile(&file);
printf("%d bytes to be encrypted\n", (int)fileLength);
-
+
// Encrypt the file
unsigned char *encryptedFile;
int encryptedFileLength;
- if((encryptedFileLength = aesEncrypt(aesEncryptCtx, aesKey, aesIV, (const unsigned char*)file, fileLength, &encryptedFile)) == -1) {
- fprintf(stderr, "Encryption failed\n");
- return 1;
- }
- printf("%d bytes encrypted\n", encryptedFileLength);
-
- // Append .enc to the filename
- char *encryptedFilename = (char*)malloc(strlen(filename) + 5);
- if(encryptedFilename == NULL) {
- fprintf(stderr, "Failed to allocate memory\n");
- return 1;
- }
- sprintf(encryptedFilename, "%s.enc", filename);
-
- // Write the encrypted file to its own file
- writeFile(encryptedFilename, encryptedFile, encryptedFileLength);
- std::cerr << "aesKey : ";
- for (int i = 0; i < 32; i++) {
- std::cerr << std::hex << aesKey[i];
- }
- std::cerr << std::endl;
+ crypt temp = static_cast(O);
+
+ // if((encryptedFileLength = (temp).aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
+ // fprintf(stderr, "Encryption failed\n");
+ // return 1;
+ // }
+ // printf("%d bytes encrypted\n", encryptedFileLength);
- printf("Encrypted message written to \"%s\"\n", encryptedFilename);
+ // // Append .enc to the filename
+ // char *encryptedFilename = (char*)malloc(strlen(filename) + 5);
+ // if(encryptedFilename == NULL) {
+ // fprintf(stderr, "Failed to allocate memory\n");
+ // return 1;
+ // }
+ // sprintf(encryptedFilename, "%s.enc", filename);
+
+ // // Write the encrypted file to its own file
+ // writeFile(encryptedFilename, encryptedFile, encryptedFileLength);
+
+ // std::cerr << "aesKey : ";
+ // for (int i = 0; i < 32; i++) {
+ // std::cerr << std::hex << aesKey[i];
+ // }
+ // std::cerr << std::endl;
+
+ // printf("Encrypted message written to \"%s\"\n", encryptedFilename);
free(file);
- //***************************************************
+ // //***************************************************
- fileLength = readFile(encryptedFilename, &file);
+ // fileLength = readFile(encryptedFilename, &file);
- // Decrypt the encrypted file
- unsigned char *decryptedFile;
- int decryptedFileLength;
- if((decryptedFileLength = aesDecrypt(aesDecryptCtx, aesKey, aesIV, file, fileLength, &decryptedFile)) == -1) {
- fprintf(stderr, "Decryption failed\n");
- return 1;
- }
- printf("%d bytes decrypted\n", (int)decryptedFileLength);
+ // // Decrypt the encrypted file
+ // unsigned char *decryptedFile;
+ // int decryptedFileLength;
+ // if((decryptedFileLength = aesDecrypt(aesDecryptCtx, aesKey, aesIV, file, fileLength, &decryptedFile)) == -1) {
+ // fprintf(stderr, "Decryption failed\n");
+ // return 1;
+ // }
+ // printf("%d bytes decrypted\n", (int)decryptedFileLength);
- // Append .dec to the filename
- char *decryptedFilename = (char*)malloc(strlen(filename) + 5);
- if(decryptedFilename == NULL) {
- fprintf(stderr, "Failed to allocate memory\n");
- return 1;
- }
- sprintf(decryptedFilename, "%s.dec", filename);
+ // // Append .dec to the filename
+ // char *decryptedFilename = (char*)malloc(strlen(filename) + 5);
+ // if(decryptedFilename == NULL) {
+ // fprintf(stderr, "Failed to allocate memory\n");
+ // return 1;
+ // }
+ // sprintf(decryptedFilename, "%s.dec", filename);
- // Write the decrypted file to its own file
- writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
- printf("Decrypted file written to \"%s\"\n", decryptedFilename);
+ // // Write the decrypted file to its own file
+ // writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
+ // printf("Decrypted file written to \"%s\"\n", decryptedFilename);
- free(decryptedFile);
- free(file);
+ // free(decryptedFile);
+ // free(file);
-
- //***************************************************
- clear_all(aesEncryptCtx, aesDecryptCtx, aesKey, aesIV);
return 0;
}
\ No newline at end of file
diff --git a/aes.cpp.save b/aes.cpp.save
new file mode 100644
index 0000000..c6952e9
--- /dev/null
+++ b/aes.cpp.save
@@ -0,0 +1,256 @@
+#include
+#include
+#include
+#include
+
+#include
+#include
+#include
+#include
+#include
+
+#define FAILURE -1
+#define SUCCESS 0
+
+#define AES_KEYLEN 256
+#define AES_ROUNDS 6
+
+
+int aesDecrypt(EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV, unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg) {
+ size_t decLen = 0;
+ size_t blockLen = 0;
+
+ *decMsg = (unsigned char*)malloc(encMsgLen);
+ if(*decMsg == NULL) return FAILURE;
+
+ if(!EVP_DecryptInit_ex(aesDecryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
+ return FAILURE;
+ }
+
+ if(!EVP_DecryptUpdate(aesDecryptCtx, (unsigned char*)*decMsg, (int*)&blockLen, encMsg, (int)encMsgLen)) {
+ return FAILURE;
+ }
+ decLen += blockLen;
+
+ if(!EVP_DecryptFinal_ex(aesDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
+ return FAILURE;
+ }
+ decLen += blockLen;
+
+ EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
+
+ return (int)decLen;
+}
+
+int aesEncrypt(EVP_CIPHER_CTX* aesEncryptCtx, unsigned char* aesKey, unsigned char* aesIV, const unsigned char *msg, size_t msgLen, unsigned char **encMsg) {
+ size_t blockLen = 0;
+ size_t encMsgLen = 0;
+
+ *encMsg = (unsigned char*)malloc(msgLen + AES_BLOCK_SIZE);
+ if(encMsg == NULL) return FAILURE;
+
+ if(!EVP_EncryptInit_ex(aesEncryptCtx, EVP_aes_256_cbc(), NULL, aesKey, aesIV)) {
+ return FAILURE;
+ }
+
+ if(!EVP_EncryptUpdate(aesEncryptCtx, *encMsg, (int*)&blockLen, (unsigned char*)msg, msgLen)) {
+ return FAILURE;
+ }
+ encMsgLen += blockLen;
+
+ if(!EVP_EncryptFinal_ex(aesEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen)) {
+ return FAILURE;
+ }
+
+ EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
+
+ return encMsgLen + blockLen;
+}
+
+void clear_all(EVP_CIPHER_CTX* aesEncryptCtx, EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV) {
+ EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
+ EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
+
+ free(aesEncryptCtx);
+ free(aesDecryptCtx);
+
+ free(aesIV);
+ free(aesKey);
+}
+
+void init_all(EVP_CIPHER_CTX* aesEncryptCtx, EVP_CIPHER_CTX* aesDecryptCtx, unsigned char* aesKey, unsigned char* aesIV) {
+ EVP_CIPHER_CTX_init(aesEncryptCtx);
+ EVP_CIPHER_CTX_init(aesDecryptCtx);
+
+ unsigned char *aesPass = (unsigned char*)malloc(AES_KEYLEN/8);
+ unsigned char *aesSalt = (unsigned char*)malloc(8);
+
+ if(aesKey == NULL || aesIV == NULL || aesPass == NULL || aesSalt == NULL) {
+ exit(FAILURE);
+ }
+
+ #define USE_PBKDF
+ #ifdef USE_PBKDF
+ std::cerr << "utilisation de USE_PBKDF" << std::endl;
+ // Get some random data to use as the AES pass and salt
+ if(RAND_bytes(aesPass, AES_KEYLEN/8) == 0) {
+ exit(FAILURE);
+ }
+
+ if(RAND_bytes(aesSalt, 8) == 0) {
+ exit(FAILURE);
+ }
+
+ if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, AES_KEYLEN/8, AES_ROUNDS, aesKey, aesIV) == 0) {
+ exit(FAILURE);
+ }
+ #else
+ if(RAND_bytes(aesKey, AES_KEYLEN/8) == 0) {
+ exit(FAILURE);
+ }
+
+ if(RAND_bytes(aesIV, AES_KEYLEN/8) == 0) {
+ exit(FAILURE);
+ }
+ #endif
+
+ free(aesPass);
+ free(aesSalt);
+}
+
+void writeFile(char *filename, unsigned char *file, size_t fileLength) {
+ FILE *fd = fopen(filename, "wb");
+ if(fd == NULL) {
+ fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ size_t bytesWritten = fwrite(file, 1, fileLength, fd);
+
+ if(bytesWritten != fileLength) {
+ fprintf(stderr, "Failed to write file\n");
+ exit(1);
+ }
+
+ fclose(fd);
+}
+
+int readFile(char *filename, unsigned char **file) {
+ FILE *fd = fopen(filename, "rb");
+ if(fd == NULL) {
+ fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ // Determine size of the file
+ fseek(fd, 0, SEEK_END);
+ size_t fileLength = ftell(fd);
+ fseek(fd, 0, SEEK_SET);
+
+ // Allocate space for the file
+ *file = (unsigned char*)malloc(fileLength);
+ if(*file == NULL) {
+ fprintf(stderr, "Failed to allocate memory\n");
+ exit(1);
+ }
+
+ // Read the file into the buffer
+ size_t bytesRead = fread(*file, 1, fileLength, fd);
+
+ if(bytesRead != fileLength) {
+ fprintf(stderr, "Error reading file\n");
+ exit(1);
+ }
+
+ fclose(fd);
+
+ return fileLength;
+}
+
+int main(int argc, char* argv[]) {
+ if(argc != 2) {
+ fprintf(stderr, "No file argument supplied.\n");
+ return 1;
+ }
+
+ EVP_CIPHER_CTX* aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
+ EVP_CIPHER_CTX* aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
+ if(aesDecryptCtx == NULL || aesEncryptCtx == NULL) {
+ return FAILURE;
+ }
+
+ unsigned char* aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
+ unsigned char* aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
+
+ init_all(aesEncryptCtx, aesDecryptCtx, aesKey, aesIV);
+
+ //***************************************************
+ char* filename = argv[1];
+
+ // Read the file to encrypt
+ unsigned char *file;
+ size_t fileLength = readFile(filename, &file);
+ printf("%d bytes to be encrypted\n", (int)fileLength);
+
+ // Encrypt the file
+ unsigned char *encryptedFile;
+ int encryptedFileLength;
+ if((encryptedFileLength = aesEncrypt(aesEncryptCtx, aesKey, aesIV, (const unsigned char*)file, fileLength, &encryptedFile)) == -1) {
+ fprintf(stderr, "Encryption failed\n");
+ return 1;
+ }
+ printf("%d bytes encrypted\n", encryptedFileLength);
+
+ // Append .enc to the filename
+ char *encryptedFilename = (char*)malloc(strlen(filename) + 5);
+ if(encryptedFilename == NULL) {
+ fprintf(stderr, "Failed to allocate memory\n");
+ return 1;
+ }
+ sprintf(encryptedFilename, "%s.enc", filename);
+
+ // Write the encrypted file to its own file
+ writeFile(encryptedFilename, encryptedFile, encryptedFileLength);
+
+ std::cerr << "aesKey : ";
+ for (int i = 0; i < 32; i++) {
+ std::cerr << std::hex << aesKey[i];
+ }
+ std::cerr << std::endl;
+
+ printf("Encrypted message written to \"%s\"\n", encryptedFilename);
+
+ free(file);
+ //***************************************************
+
+ fileLength = readFile(encryptedFilename, &file);
+
+ // Decrypt the encrypted file
+ unsigned char *decryptedFile;
+ int decryptedFileLength;
+ if((decryptedFileLength = aesDecrypt(aesDecryptCtx, aesKey, aesIV, file, fileLength, &decryptedFile)) == -1) {
+ fprintf(stderr, "Decryption failed\n");
+ return 1;
+ }
+ printf("%d bytes decrypted\n", (int)decryptedFileLength);
+
+ // Append .dec to the filename
+ char *decryptedFilename = (char*)malloc(strlen(filename) + 5);
+ if(decryptedFilename == NULL) {
+ fprintf(stderr, "Failed to allocate memory\n");
+ return 1;
+ }
+ sprintf(decryptedFilename, "%s.dec", filename);
+
+ // Write the decrypted file to its own file
+ writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
+ printf("Decrypted file written to \"%s\"\n", decryptedFilename);
+
+ free(decryptedFile);
+ free(file);
+
+
+ //***************************************************
+ clear_all(aesEncryptCtx, aesDecryptCtx, aesKey, aesIV);
+ return 0;
+}
\ No newline at end of file
diff --git a/aes.hpp b/aes.hpp
index e69de29..7bb6ffd 100644
--- a/aes.hpp
+++ b/aes.hpp
@@ -0,0 +1,143 @@
+#ifndef _AES
+#define _AES
+
+#include
+#include
+#include
+#include
+
+#include
+#include
+#include
+#include
+#include
+
+#define FAILURE -1
+#define SUCCESS 0
+
+#define AES_KEYLEN 256
+#define AES_ROUNDS 6
+
+class aes {
+protected:
+ unsigned char* aesKey;
+ unsigned char* aesIV;
+private:
+ std::string filename;
+
+public:
+ aes(std::string _filename);
+ aes(const aes& _e);
+ void init_all();
+ void writeFile(unsigned char *file, size_t fileLength);
+ int readFile(unsigned char **file);
+ void clear_all();
+ ~aes();
+};
+
+aes::aes(std::string _filename) {
+ filename = _filename;
+ aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
+ aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
+}
+
+aes::aes(const aes& _e) : filename(_e.filename) {
+ std::cerr << "salut\n";
+}
+
+void aes::init_all() {
+
+ unsigned char *aesPass = (unsigned char*)malloc(AES_KEYLEN/8);
+ unsigned char *aesSalt = (unsigned char*)malloc(8);
+
+ if(aesKey == NULL || aesIV == NULL || aesPass == NULL || aesSalt == NULL) {
+ exit(FAILURE);
+ }
+
+ #define USE_PBKDF
+ #ifdef USE_PBKDF
+ std::cerr << "utilisation de USE_PBKDF" << std::endl;
+ // Get some random data to use as the AES pass and salt
+ if(RAND_bytes(aesPass, AES_KEYLEN/8) == 0) {
+ exit(FAILURE);
+ }
+
+ if(RAND_bytes(aesSalt, 8) == 0) {
+ exit(FAILURE);
+ }
+
+ if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, AES_KEYLEN/8, AES_ROUNDS, aesKey, aesIV) == 0) {
+ exit(FAILURE);
+ }
+ #else
+ if(RAND_bytes(aesKey, AES_KEYLEN/8) == 0) {
+ exit(FAILURE);
+ }
+
+ if(RAND_bytes(aesIV, AES_KEYLEN/8) == 0) {
+ exit(FAILURE);
+ }
+ #endif
+
+ free(aesPass);
+ free(aesSalt);
+}
+
+// peut être déporté le buffer lut avec methode pour travaillé dessus
+int aes::readFile(unsigned char **file) {
+ FILE *fd = fopen(filename.c_str(), "rb");
+ if(fd == NULL) {
+ fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ // Determine size of the file
+ fseek(fd, 0, SEEK_END);
+ size_t fileLength = ftell(fd);
+ fseek(fd, 0, SEEK_SET);
+
+ // Allocate space for the file
+ *file = (unsigned char*)malloc(fileLength);
+ if(*file == NULL) {
+ fprintf(stderr, "Failed to allocate memory\n");
+ exit(1);
+ }
+
+ // Read the file into the buffer
+ size_t bytesRead = fread(*file, 1, fileLength, fd);
+
+ if(bytesRead != fileLength) {
+ fprintf(stderr, "Error reading file\n");
+ exit(1);
+ }
+
+ fclose(fd);
+
+ return fileLength;
+}
+
+void aes::writeFile(unsigned char *file, size_t fileLength) {
+ FILE *fd = fopen(filename.c_str(), "wb");
+ if(fd == NULL) {
+ fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ size_t bytesWritten = fwrite(file, 1, fileLength, fd);
+
+ if(bytesWritten != fileLength) {
+ fprintf(stderr, "Failed to write file\n");
+ exit(1);
+ }
+
+ fclose(fd);
+}
+
+void aes::clear_all() {
+ free(aesIV);
+ free(aesKey);
+}
+
+aes::~aes() {}
+
+#endif
\ No newline at end of file
diff --git a/crypt.hpp b/crypt.hpp
new file mode 100644
index 0000000..1c4f123
--- /dev/null
+++ b/crypt.hpp
@@ -0,0 +1,75 @@
+#ifndef _CRYPT
+#define _CRYPT
+
+#include "aes.hpp"
+
+class crypt : public aes {
+private:
+ EVP_CIPHER_CTX* aesEncryptCtx;
+
+public:
+ crypt(std::string filename);
+ crypt(const aes& a);
+ void init_all();
+ int aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg);
+ ~crypt();
+};
+
+crypt::crypt(std::string filename) : aes(filename) {
+ aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
+ if(aesEncryptCtx == NULL) {
+ exit(FAILURE);
+ }
+ init_all();
+}
+
+crypt::crypt(const aes& _a) : aes(_a) {
+ aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
+ if(aesEncryptCtx == NULL) {
+ exit(FAILURE);
+ }
+ int res = EVP_CIPHER_CTX_copy(aesEncryptCtx, static_cast(_a).aesEncryptCtx);
+ if(!res)
+ exit(FAILURE);
+
+ // *aesEncryptCtx = (*(static_cast(_a)).aesEncryptCtx);
+}
+
+void crypt::init_all() {
+ aes::init_all();
+ EVP_CIPHER_CTX_init(aesEncryptCtx);
+}
+
+int crypt::aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg) {
+ size_t blockLen = 0;
+ size_t encMsgLen = 0;
+
+ *encMsg = (unsigned char*)malloc(msgLen + AES_BLOCK_SIZE);
+ if(encMsg == NULL) return FAILURE;
+
+ if(!EVP_EncryptInit_ex(aesEncryptCtx, EVP_aes_256_cbc(), NULL, aes::aesKey, aes::aesIV)) {
+ return FAILURE;
+ }
+
+ if(!EVP_EncryptUpdate(aesEncryptCtx, *encMsg, (int*)&blockLen, (unsigned char*)msg, msgLen)) {
+ return FAILURE;
+ }
+ encMsgLen += blockLen;
+
+ if(!EVP_EncryptFinal_ex(aesEncryptCtx, *encMsg + encMsgLen, (int*)&blockLen)) {
+ return FAILURE;
+ }
+
+ EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
+
+ return encMsgLen + blockLen;
+}
+
+
+crypt::~crypt() {
+ aes::clear_all();
+ EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
+ free(aesEncryptCtx);
+}
+
+#endif
\ No newline at end of file
diff --git a/crypto.ex b/crypto.ex
index 8c1725c..082483d 100755
Binary files a/crypto.ex and b/crypto.ex differ
diff --git a/decrypt.hpp b/decrypt.hpp
new file mode 100644
index 0000000..9a78c83
--- /dev/null
+++ b/decrypt.hpp
@@ -0,0 +1,71 @@
+#ifndef _DECRYPT
+#define _DECRYPT
+
+#include "aes.hpp"
+
+class decrypt : public aes {
+private:
+ EVP_CIPHER_CTX* aesDecryptCtx;
+public:
+ decrypt(std::string filename);
+ decrypt(const aes& a);
+ void init_all();
+ int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg);
+ ~decrypt();
+};
+
+
+decrypt::decrypt(std::string filename) : aes(filename) {
+ aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
+ if(aesDecryptCtx == NULL) {
+ exit(FAILURE);
+ }
+ init_all();
+}
+
+decrypt::decrypt(const aes& _a) : aes(_a) {
+ aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
+ if(aesDecryptCtx == NULL) {
+ exit(FAILURE);
+ }
+ *aesDecryptCtx = (*(static_cast(_a)).aesDecryptCtx);
+}
+
+void decrypt::init_all() {
+ aes::init_all();
+ EVP_CIPHER_CTX_init(aesDecryptCtx);
+}
+
+int decrypt::aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg) {
+ size_t decLen = 0;
+ size_t blockLen = 0;
+
+ *decMsg = (unsigned char*)malloc(encMsgLen);
+ if(*decMsg == NULL) return FAILURE;
+
+ if(!EVP_DecryptInit_ex(aesDecryptCtx, EVP_aes_256_cbc(), NULL, aes::aesKey, aes::aesIV)) {
+ return FAILURE;
+ }
+
+ if(!EVP_DecryptUpdate(aesDecryptCtx, (unsigned char*)*decMsg, (int*)&blockLen, encMsg, (int)encMsgLen)) {
+ return FAILURE;
+ }
+ decLen += blockLen;
+
+ if(!EVP_DecryptFinal_ex(aesDecryptCtx, (unsigned char*)*decMsg + decLen, (int*)&blockLen)) {
+ return FAILURE;
+ }
+ decLen += blockLen;
+
+ EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
+
+ return (int)decLen;
+}
+
+decrypt::~decrypt() {
+ aes::clear_all();
+ EVP_CIPHER_CTX_cleanup(aesDecryptCtx);
+ free(aesDecryptCtx);
+}
+
+#endif
\ No newline at end of file
diff --git a/makefile b/makefile
index d2e7722..1775626 100644
--- a/makefile
+++ b/makefile
@@ -1,5 +1,7 @@
main:
@g++ -Wall -Wextra -ggdb -o crypto.ex aes.cpp -lcrypto
+
+run:
@./crypto.ex file.txt
clean: