71 lines
2.2 KiB
C++
71 lines
2.2 KiB
C++
#include "aes.hpp"
|
|
#include "crypt.hpp"
|
|
#include "decrypt.hpp"
|
|
|
|
int main(int argc, char* argv[]) {
|
|
if(argc != 2) {
|
|
std::cerr << "No file argument supplied.\n";
|
|
return 1;
|
|
}
|
|
|
|
//***************************************************
|
|
char* filename = argv[1];
|
|
|
|
aes* O = new crypt(filename);
|
|
|
|
// Read the file to encrypt
|
|
unsigned char* 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 = (dynamic_cast<crypt*>(O))->aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
|
|
fprintf(stderr, "Encryption failed\n");
|
|
return 1;
|
|
}
|
|
|
|
printf("%d bytes encrypted\n", encryptedFileLength);
|
|
std::cerr << O->PrintAesKey() << std::endl;
|
|
std::string keyFilename = O->exportKey();
|
|
printf("Key written to \"%s\"\n", keyFilename.c_str());
|
|
|
|
// 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 = O2->readFile(&file);
|
|
std::cerr << "byte to be dectypted : " << fileLength << std::endl;
|
|
|
|
// Decrypt the encrypted file
|
|
unsigned char* decryptedFile;
|
|
|
|
int decryptedFileLength;
|
|
if((decryptedFileLength = (dynamic_cast<decrypt*>(O2))->aesDecrypt(file, fileLength, &decryptedFile)) == -1) {
|
|
fprintf(stderr, "Decryption failed\n");
|
|
return 1;
|
|
}
|
|
|
|
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);
|
|
delete O2;
|
|
delete O;
|
|
|
|
return 0;
|
|
}
|