fonctionne
This commit is contained in:
parent
a96729198f
commit
103fb7d640
84
aes.cpp
84
aes.cpp
@ -11,73 +11,57 @@ int main(int argc, char* argv[]) {
|
|||||||
//***************************************************
|
//***************************************************
|
||||||
char* filename = argv[1];
|
char* filename = argv[1];
|
||||||
|
|
||||||
aes O = crypt(filename);
|
aes* O = new crypt(filename);
|
||||||
|
|
||||||
// Read the file to encrypt
|
// Read the file to encrypt
|
||||||
unsigned char *file;
|
unsigned char* file;
|
||||||
// readFile fait l'aloccation mémoire !!! pensé au free
|
// readFile fait l'aloccation mémoire !!! pensé au free
|
||||||
size_t fileLength = O.readFile(&file);
|
size_t fileLength = O->readFile(&file);
|
||||||
printf("%d bytes to be encrypted\n", (int)fileLength);
|
printf("%d bytes to be encrypted\n", (int)fileLength);
|
||||||
|
|
||||||
// Encrypt the file
|
// Encrypt the file
|
||||||
unsigned char *encryptedFile;
|
unsigned char *encryptedFile;
|
||||||
int encryptedFileLength;
|
int encryptedFileLength;
|
||||||
|
|
||||||
crypt temp = static_cast<crypt>(O);
|
if((encryptedFileLength = (dynamic_cast<crypt*>(O))->aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
|
||||||
|
fprintf(stderr, "Encryption failed\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// if((encryptedFileLength = (temp).aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
|
printf("%d bytes encrypted\n", encryptedFileLength);
|
||||||
// fprintf(stderr, "Encryption failed\n");
|
std::cerr << O->PrintAesKey() << std::endl;
|
||||||
// return 1;
|
// Write the encrypted file to its own file
|
||||||
// }
|
std::string encryptedFilename = O->writeFile(encryptedFile, encryptedFileLength);
|
||||||
// printf("%d bytes encrypted\n", encryptedFileLength);
|
printf("Encrypted message written to \"%s\"\n", encryptedFilename.c_str());
|
||||||
|
|
||||||
// // 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(encryptedFile);
|
||||||
free(file);
|
free(file);
|
||||||
// //***************************************************
|
//***************************************************
|
||||||
|
std::cerr << "*******************************" << std::endl;
|
||||||
|
aes* O2 = new decrypt(encryptedFilename.c_str(), O->getAesKey(), O->getAesIV());
|
||||||
|
|
||||||
// fileLength = readFile(encryptedFilename, &file);
|
fileLength = O2->readFile(&file);
|
||||||
|
std::cerr << "byte to be dectypted : " << fileLength << std::endl;
|
||||||
|
|
||||||
// // Decrypt the encrypted file
|
// Decrypt the encrypted file
|
||||||
// unsigned char *decryptedFile;
|
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
|
int decryptedFileLength;
|
||||||
// char *decryptedFilename = (char*)malloc(strlen(filename) + 5);
|
if((decryptedFileLength = (dynamic_cast<decrypt*>(O2))->aesDecrypt(file, fileLength, &decryptedFile)) == -1) {
|
||||||
// if(decryptedFilename == NULL) {
|
fprintf(stderr, "Decryption failed\n");
|
||||||
// fprintf(stderr, "Failed to allocate memory\n");
|
return 1;
|
||||||
// return 1;
|
}
|
||||||
// }
|
|
||||||
// sprintf(decryptedFilename, "%s.dec", filename);
|
|
||||||
|
|
||||||
// // Write the decrypted file to its own file
|
printf("%d bytes decrypted\n", (int)decryptedFileLength);
|
||||||
// writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
|
std::cerr << O2->PrintAesKey() << std::endl;
|
||||||
// printf("Decrypted file written to \"%s\"\n", decryptedFilename);
|
// Write the decrypted file to its own file
|
||||||
|
std::string decryptedFilename = O2->writeFile(decryptedFile, decryptedFileLength);
|
||||||
|
printf("Decrypted file written to \"%s\"\n", decryptedFilename.c_str());
|
||||||
|
|
||||||
// free(decryptedFile);
|
free(decryptedFile);
|
||||||
// free(file);
|
free(file);
|
||||||
|
delete O2;
|
||||||
|
delete O;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
94
aes.hpp
94
aes.hpp
@ -4,7 +4,11 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <typeinfo>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
#include <openssl/pem.h>
|
#include <openssl/pem.h>
|
||||||
@ -22,17 +26,28 @@ class aes {
|
|||||||
protected:
|
protected:
|
||||||
unsigned char* aesKey;
|
unsigned char* aesKey;
|
||||||
unsigned char* aesIV;
|
unsigned char* aesIV;
|
||||||
|
aes& clone(const aes& _e);
|
||||||
|
void init_all();
|
||||||
|
void clear_all();
|
||||||
|
void setExtension(std::string _ex);
|
||||||
private:
|
private:
|
||||||
std::string filename;
|
std::string filename;
|
||||||
|
std::string generateNewFilename();
|
||||||
|
std::string extension = ".new";
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
aes() {};
|
||||||
aes(std::string _filename);
|
aes(std::string _filename);
|
||||||
aes(const aes& _e);
|
std::string writeFile(unsigned char *file, size_t fileLength);
|
||||||
void init_all();
|
|
||||||
void writeFile(unsigned char *file, size_t fileLength);
|
|
||||||
int readFile(unsigned char **file);
|
int readFile(unsigned char **file);
|
||||||
void clear_all();
|
|
||||||
~aes();
|
std::string PrintAesKey();
|
||||||
|
void setAesIV(unsigned char* _aesIV);
|
||||||
|
void setAesKey(unsigned char* _aesKey);
|
||||||
|
unsigned char* getAesKey();
|
||||||
|
unsigned char* getAesIV();
|
||||||
|
|
||||||
|
virtual ~aes() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
aes::aes(std::string _filename) {
|
aes::aes(std::string _filename) {
|
||||||
@ -41,8 +56,13 @@ aes::aes(std::string _filename) {
|
|||||||
aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
|
aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
|
||||||
}
|
}
|
||||||
|
|
||||||
aes::aes(const aes& _e) : filename(_e.filename) {
|
aes& aes::clone(const aes& _e) {
|
||||||
std::cerr << "salut\n";
|
filename = _e.filename;
|
||||||
|
|
||||||
|
setAesKey(_e.aesKey);
|
||||||
|
setAesIV(_e.aesIV);
|
||||||
|
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void aes::init_all() {
|
void aes::init_all() {
|
||||||
@ -54,7 +74,7 @@ void aes::init_all() {
|
|||||||
exit(FAILURE);
|
exit(FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define USE_PBKDF
|
// #define USE_PBKDF
|
||||||
#ifdef USE_PBKDF
|
#ifdef USE_PBKDF
|
||||||
std::cerr << "utilisation de USE_PBKDF" << std::endl;
|
std::cerr << "utilisation de USE_PBKDF" << std::endl;
|
||||||
// Get some random data to use as the AES pass and salt
|
// Get some random data to use as the AES pass and salt
|
||||||
@ -84,7 +104,7 @@ void aes::init_all() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// peut être déporté le buffer lut avec methode pour travaillé dessus
|
// peut être déporté le buffer lut avec methode pour travaillé dessus
|
||||||
int aes::readFile(unsigned char **file) {
|
int aes::readFile(unsigned char** file) {
|
||||||
FILE *fd = fopen(filename.c_str(), "rb");
|
FILE *fd = fopen(filename.c_str(), "rb");
|
||||||
if(fd == NULL) {
|
if(fd == NULL) {
|
||||||
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
|
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
|
||||||
@ -116,8 +136,15 @@ int aes::readFile(unsigned char **file) {
|
|||||||
return fileLength;
|
return fileLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
void aes::writeFile(unsigned char *file, size_t fileLength) {
|
std::string aes::generateNewFilename() {
|
||||||
FILE *fd = fopen(filename.c_str(), "wb");
|
std::regex e("\\b(.*[(\\.txt)]*)\\.[^(txt)].*");
|
||||||
|
filename = std::regex_replace(filename, e,"$1");
|
||||||
|
return filename+extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string aes::writeFile(unsigned char *file, size_t fileLength) {
|
||||||
|
std::string newFile = generateNewFilename();
|
||||||
|
FILE *fd = fopen(newFile.c_str(), "wb");
|
||||||
if(fd == NULL) {
|
if(fd == NULL) {
|
||||||
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
|
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -131,6 +158,49 @@ void aes::writeFile(unsigned char *file, size_t fileLength) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
return newFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
void aes::setExtension(std::string _ex) {
|
||||||
|
extension = _ex;
|
||||||
|
}
|
||||||
|
|
||||||
|
void aes::setAesKey(unsigned char* _aesKey) {
|
||||||
|
if(aesKey == NULL)
|
||||||
|
aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
|
||||||
|
memcpy(aesKey , _aesKey, AES_KEYLEN/8);
|
||||||
|
}
|
||||||
|
|
||||||
|
void aes::setAesIV(unsigned char* _aesIV) {
|
||||||
|
if(aesIV == NULL)
|
||||||
|
aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
|
||||||
|
memcpy(aesIV , _aesIV, AES_KEYLEN/8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
unsigned char* aes::getAesKey() {
|
||||||
|
unsigned char* res = (unsigned char*)malloc(AES_KEYLEN/8);
|
||||||
|
memcpy(res, aesKey, AES_KEYLEN/8);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char* aes::getAesIV() {
|
||||||
|
unsigned char* res = (unsigned char*)malloc(AES_KEYLEN/8);
|
||||||
|
memcpy(res, aesIV, AES_KEYLEN/8);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string aes::PrintAesKey() {
|
||||||
|
std::stringstream ss;
|
||||||
|
std::string res;
|
||||||
|
|
||||||
|
res = "aesKey : ";
|
||||||
|
for (int i = 0; i < 32; i++) {
|
||||||
|
ss << std::hex;
|
||||||
|
ss >> res;
|
||||||
|
res += std::string(1, (char)aesKey[i]+'\n');
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void aes::clear_all() {
|
void aes::clear_all() {
|
||||||
@ -138,6 +208,4 @@ void aes::clear_all() {
|
|||||||
free(aesKey);
|
free(aesKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
aes::~aes() {}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
14
crypt.hpp
14
crypt.hpp
@ -9,7 +9,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
crypt(std::string filename);
|
crypt(std::string filename);
|
||||||
crypt(const aes& a);
|
crypt(const crypt& a);
|
||||||
void init_all();
|
void init_all();
|
||||||
int aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg);
|
int aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg);
|
||||||
~crypt();
|
~crypt();
|
||||||
@ -23,19 +23,21 @@ crypt::crypt(std::string filename) : aes(filename) {
|
|||||||
init_all();
|
init_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
crypt::crypt(const aes& _a) : aes(_a) {
|
crypt::crypt(const crypt& _a) : aes::aes() {
|
||||||
|
this->clone(_a);
|
||||||
|
|
||||||
aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
|
aesEncryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
|
||||||
if(aesEncryptCtx == NULL) {
|
if(aesEncryptCtx == NULL) {
|
||||||
exit(FAILURE);
|
exit(FAILURE);
|
||||||
}
|
}
|
||||||
int res = EVP_CIPHER_CTX_copy(aesEncryptCtx, static_cast<crypt>(_a).aesEncryptCtx);
|
|
||||||
|
int res = EVP_CIPHER_CTX_copy(aesEncryptCtx, _a.aesEncryptCtx);
|
||||||
if(!res)
|
if(!res)
|
||||||
exit(FAILURE);
|
exit(FAILURE);
|
||||||
|
|
||||||
// *aesEncryptCtx = (*(static_cast<crypt>(_a)).aesEncryptCtx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void crypt::init_all() {
|
void crypt::init_all() {
|
||||||
|
setExtension(".enc");
|
||||||
aes::init_all();
|
aes::init_all();
|
||||||
EVP_CIPHER_CTX_init(aesEncryptCtx);
|
EVP_CIPHER_CTX_init(aesEncryptCtx);
|
||||||
}
|
}
|
||||||
@ -60,8 +62,6 @@ int crypt::aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **e
|
|||||||
return FAILURE;
|
return FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
|
|
||||||
|
|
||||||
return encMsgLen + blockLen;
|
return encMsgLen + blockLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
28
decrypt.hpp
28
decrypt.hpp
@ -7,31 +7,43 @@ class decrypt : public aes {
|
|||||||
private:
|
private:
|
||||||
EVP_CIPHER_CTX* aesDecryptCtx;
|
EVP_CIPHER_CTX* aesDecryptCtx;
|
||||||
public:
|
public:
|
||||||
decrypt(std::string filename);
|
decrypt(std::string filename, unsigned char* aesKey=0, unsigned char* aesIV=0);
|
||||||
decrypt(const aes& a);
|
decrypt(const decrypt& a);
|
||||||
void init_all();
|
void init_all(bool initSuper=true);
|
||||||
int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char **decMsg);
|
int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char** decMsg);
|
||||||
~decrypt();
|
~decrypt();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
decrypt::decrypt(std::string filename) : aes(filename) {
|
decrypt::decrypt(std::string filename, unsigned char* aesKey, unsigned char* aesIV) : aes(filename) {
|
||||||
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
|
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
|
||||||
if(aesDecryptCtx == NULL) {
|
if(aesDecryptCtx == NULL) {
|
||||||
exit(FAILURE);
|
exit(FAILURE);
|
||||||
}
|
}
|
||||||
|
if(aesKey && aesIV) {
|
||||||
|
setAesKey(aesKey);
|
||||||
|
setAesIV(aesIV);
|
||||||
|
init_all(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
init_all();
|
init_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
decrypt::decrypt(const aes& _a) : aes(_a) {
|
decrypt::decrypt(const decrypt& _a) : aes::aes() {
|
||||||
|
this->clone(_a);
|
||||||
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
|
aesDecryptCtx = (EVP_CIPHER_CTX*)malloc(sizeof(EVP_CIPHER_CTX));
|
||||||
if(aesDecryptCtx == NULL) {
|
if(aesDecryptCtx == NULL) {
|
||||||
exit(FAILURE);
|
exit(FAILURE);
|
||||||
}
|
}
|
||||||
*aesDecryptCtx = (*(static_cast<decrypt>(_a)).aesDecryptCtx);
|
|
||||||
|
int res = EVP_CIPHER_CTX_copy(aesDecryptCtx, _a.aesDecryptCtx);
|
||||||
|
if(!res)
|
||||||
|
exit(FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void decrypt::init_all() {
|
void decrypt::init_all(bool initSuper) {
|
||||||
|
setExtension(".dec");
|
||||||
|
if(initSuper)
|
||||||
aes::init_all();
|
aes::init_all();
|
||||||
EVP_CIPHER_CTX_init(aesDecryptCtx);
|
EVP_CIPHER_CTX_init(aesDecryptCtx);
|
||||||
}
|
}
|
||||||
|
7
file.txt
7
file.txt
@ -1,7 +0,0 @@
|
|||||||
toto
|
|
||||||
|
|
||||||
|
|
||||||
rftyguhjio
|
|
||||||
|
|
||||||
ftyguhiokpl
|
|
||||||
ùvfdjibngflkg,kflhi,omqcgikfsngchjfdngbfopdpioijn,hgiuhnb,sd;
|
|
7
makefile
7
makefile
@ -1,8 +1,11 @@
|
|||||||
main:
|
main:
|
||||||
@g++ -Wall -Wextra -ggdb -o crypto.ex aes.cpp -lcrypto
|
@g++ -Wall -Wextra -ggdb -o crypto.ex aes.cpp -lcrypto --std=c++14
|
||||||
|
|
||||||
run:
|
run:
|
||||||
@./crypto.ex file.txt
|
@./crypto.ex file
|
||||||
|
|
||||||
|
valgrind:
|
||||||
|
@valgrind --leak-check=full --track-origins=yes ./crypto.ex file
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@rm *.ex
|
@rm *.ex
|
Loading…
Reference in New Issue
Block a user