fonctionne

This commit is contained in:
antoine 2016-01-23 22:57:15 +01:00
parent a96729198f
commit 103fb7d640
9 changed files with 155 additions and 94 deletions

84
aes.cpp
View File

@ -11,73 +11,57 @@ int main(int argc, char* argv[]) {
//***************************************************
char* filename = argv[1];
aes O = crypt(filename);
aes* O = new crypt(filename);
// Read the file to encrypt
unsigned char *file;
unsigned char* file;
// 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);
// Encrypt the file
unsigned char *encryptedFile;
int encryptedFileLength;
crypt temp = static_cast<crypt>(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);
if((encryptedFileLength = (dynamic_cast<crypt*>(O))->aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
fprintf(stderr, "Encryption failed\n");
return 1;
}
// // 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);
printf("%d bytes encrypted\n", encryptedFileLength);
std::cerr << O->PrintAesKey() << std::endl;
// Write the encrypted file to its own file
std::string encryptedFilename = O->writeFile(encryptedFile, encryptedFileLength);
printf("Encrypted message written to \"%s\"\n", encryptedFilename.c_str());
free(encryptedFile);
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
// 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;
// // 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);
int decryptedFileLength;
if((decryptedFileLength = (dynamic_cast<decrypt*>(O2))->aesDecrypt(file, fileLength, &decryptedFile)) == -1) {
fprintf(stderr, "Decryption failed\n");
return 1;
}
// // Write the decrypted file to its own file
// writeFile(decryptedFilename, decryptedFile, decryptedFileLength);
// printf("Decrypted file written to \"%s\"\n", decryptedFilename);
printf("%d bytes decrypted\n", (int)decryptedFileLength);
std::cerr << O2->PrintAesKey() << std::endl;
// 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(file);
free(decryptedFile);
free(file);
delete O2;
delete O;
return 0;
}

96
aes.hpp
View File

@ -4,7 +4,11 @@
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <string.h>
#include <algorithm>
#include <typeinfo>
#include <regex>
#include <openssl/evp.h>
#include <openssl/pem.h>
@ -22,17 +26,28 @@ class aes {
protected:
unsigned char* aesKey;
unsigned char* aesIV;
aes& clone(const aes& _e);
void init_all();
void clear_all();
void setExtension(std::string _ex);
private:
std::string filename;
std::string generateNewFilename();
std::string extension = ".new";
public:
aes() {};
aes(std::string _filename);
aes(const aes& _e);
void init_all();
void writeFile(unsigned char *file, size_t fileLength);
std::string writeFile(unsigned char *file, size_t fileLength);
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) {
@ -41,8 +56,13 @@ aes::aes(std::string _filename) {
aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
}
aes::aes(const aes& _e) : filename(_e.filename) {
std::cerr << "salut\n";
aes& aes::clone(const aes& _e) {
filename = _e.filename;
setAesKey(_e.aesKey);
setAesIV(_e.aesIV);
return *this;
}
void aes::init_all() {
@ -54,7 +74,7 @@ void aes::init_all() {
exit(FAILURE);
}
#define USE_PBKDF
// #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
@ -84,7 +104,7 @@ void aes::init_all() {
}
// 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");
if(fd == NULL) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
@ -102,7 +122,7 @@ int aes::readFile(unsigned char **file) {
fprintf(stderr, "Failed to allocate memory\n");
exit(1);
}
// Read the file into the buffer
size_t bytesRead = fread(*file, 1, fileLength, fd);
@ -116,8 +136,15 @@ int aes::readFile(unsigned char **file) {
return fileLength;
}
void aes::writeFile(unsigned char *file, size_t fileLength) {
FILE *fd = fopen(filename.c_str(), "wb");
std::string aes::generateNewFilename() {
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) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
exit(1);
@ -131,6 +158,49 @@ void aes::writeFile(unsigned char *file, size_t fileLength) {
}
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() {
@ -138,6 +208,4 @@ void aes::clear_all() {
free(aesKey);
}
aes::~aes() {}
#endif

View File

@ -9,7 +9,7 @@ private:
public:
crypt(std::string filename);
crypt(const aes& a);
crypt(const crypt& a);
void init_all();
int aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **encMsg);
~crypt();
@ -23,19 +23,21 @@ crypt::crypt(std::string filename) : aes(filename) {
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));
if(aesEncryptCtx == NULL) {
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)
exit(FAILURE);
// *aesEncryptCtx = (*(static_cast<crypt>(_a)).aesEncryptCtx);
}
void crypt::init_all() {
setExtension(".enc");
aes::init_all();
EVP_CIPHER_CTX_init(aesEncryptCtx);
}
@ -46,7 +48,7 @@ int crypt::aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **e
*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;
}
@ -60,8 +62,6 @@ int crypt::aesEncrypt(const unsigned char *msg, size_t msgLen, unsigned char **e
return FAILURE;
}
EVP_CIPHER_CTX_cleanup(aesEncryptCtx);
return encMsgLen + blockLen;
}

BIN
crypto.ex

Binary file not shown.

View File

@ -7,32 +7,44 @@ 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(std::string filename, unsigned char* aesKey=0, unsigned char* aesIV=0);
decrypt(const decrypt& a);
void init_all(bool initSuper=true);
int aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char** decMsg);
~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));
if(aesDecryptCtx == NULL) {
exit(FAILURE);
}
init_all();
if(aesKey && aesIV) {
setAesKey(aesKey);
setAesIV(aesIV);
init_all(false);
}
else
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));
if(aesDecryptCtx == NULL) {
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() {
aes::init_all();
void decrypt::init_all(bool initSuper) {
setExtension(".dec");
if(initSuper)
aes::init_all();
EVP_CIPHER_CTX_init(aesDecryptCtx);
}
@ -42,7 +54,7 @@ int decrypt::aesDecrypt(unsigned char *encMsg, size_t encMsgLen, unsigned char *
*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;
}

1
file Normal file
View File

@ -0,0 +1 @@
toto

View File

@ -1,7 +0,0 @@
toto
rftyguhjio
ftyguhiokpl
ùvfdjibngflkg,kflhi,omqcgikfsngchjfdngbfopdpioijn,hgiuhnb,sd;

View File

@ -1,8 +1,11 @@
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:
@./crypto.ex file.txt
@./crypto.ex file
valgrind:
@valgrind --leak-check=full --track-origins=yes ./crypto.ex file
clean:
@rm *.ex
@rm *.ex