aes/main.cpp

71 lines
2.2 KiB
C++
Raw Normal View History

2016-01-20 19:25:02 +00:00
#include "aes.hpp"
#include "crypt.hpp"
#include "decrypt.hpp"
int main(int argc, char* argv[]) {
if(argc != 2) {
2016-01-20 19:25:02 +00:00
std::cerr << "No file argument supplied.\n";
return 1;
}
//***************************************************
char* filename = argv[1];
2016-01-23 21:57:15 +00:00
aes* O = new crypt(filename);
2016-01-20 19:25:02 +00:00
// Read the file to encrypt
2016-01-23 21:57:15 +00:00
unsigned char* file;
2016-01-20 19:25:02 +00:00
// readFile fait l'aloccation mémoire !!! pensé au free
2016-01-23 21:57:15 +00:00
size_t fileLength = O->readFile(&file);
printf("%d bytes to be encrypted\n", (int)fileLength);
// Encrypt the file
unsigned char *encryptedFile;
int encryptedFileLength;
2016-01-23 21:57:15 +00:00
if((encryptedFileLength = (dynamic_cast<crypt*>(O))->aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
fprintf(stderr, "Encryption failed\n");
return 1;
}
2016-01-23 21:57:15 +00:00
printf("%d bytes encrypted\n", encryptedFileLength);
std::cerr << O->PrintAesKey() << std::endl;
2020-04-27 00:02:12 +00:00
std::string keyFilename = O->exportKey();
printf("Key written to \"%s\"\n", keyFilename.c_str());
2016-01-23 21:57:15 +00:00
// 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());
2016-01-23 21:57:15 +00:00
free(encryptedFile);
free(file);
2016-01-23 21:57:15 +00:00
//***************************************************
std::cerr << "*******************************" << std::endl;
aes* O2 = new decrypt(encryptedFilename.c_str(), O->getAesKey(), O->getAesIV());
2016-01-20 19:25:02 +00:00
2016-01-23 21:57:15 +00:00
fileLength = O2->readFile(&file);
std::cerr << "byte to be dectypted : " << fileLength << std::endl;
2016-01-20 19:25:02 +00:00
2016-01-23 21:57:15 +00:00
// Decrypt the encrypted file
unsigned char* decryptedFile;
2016-01-20 19:25:02 +00:00
2016-01-23 21:57:15 +00:00
int decryptedFileLength;
if((decryptedFileLength = (dynamic_cast<decrypt*>(O2))->aesDecrypt(file, fileLength, &decryptedFile)) == -1) {
fprintf(stderr, "Decryption failed\n");
return 1;
}
2016-01-23 21:57:15 +00:00
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());
2016-01-23 21:57:15 +00:00
free(decryptedFile);
free(file);
delete O2;
delete O;
return 0;
}