password manager with aes encryption
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.2KB

  1. #include "aes.hpp"
  2. #include "crypt.hpp"
  3. #include "decrypt.hpp"
  4. int main(int argc, char* argv[]) {
  5. if(argc != 2) {
  6. std::cerr << "No file argument supplied.\n";
  7. return 1;
  8. }
  9. //***************************************************
  10. char* filename = argv[1];
  11. aes* O = new crypt(filename);
  12. // Read the file to encrypt
  13. unsigned char* file;
  14. // readFile fait l'aloccation mémoire !!! pensé au free
  15. size_t fileLength = O->readFile(&file);
  16. printf("%d bytes to be encrypted\n", (int)fileLength);
  17. // Encrypt the file
  18. unsigned char *encryptedFile;
  19. int encryptedFileLength;
  20. if((encryptedFileLength = (dynamic_cast<crypt*>(O))->aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
  21. fprintf(stderr, "Encryption failed\n");
  22. return 1;
  23. }
  24. printf("%d bytes encrypted\n", encryptedFileLength);
  25. std::cerr << O->PrintAesKey() << std::endl;
  26. std::string keyFilename = O->exportKey();
  27. printf("Key written to \"%s\"\n", keyFilename.c_str());
  28. // Write the encrypted file to its own file
  29. std::string encryptedFilename = O->writeFile(encryptedFile, encryptedFileLength);
  30. printf("Encrypted message written to \"%s\"\n", encryptedFilename.c_str());
  31. free(encryptedFile);
  32. free(file);
  33. //***************************************************
  34. std::cerr << "*******************************" << std::endl;
  35. aes* O2 = new decrypt(encryptedFilename.c_str(), O->getAesKey(), O->getAesIV());
  36. fileLength = O2->readFile(&file);
  37. std::cerr << "byte to be dectypted : " << fileLength << std::endl;
  38. // Decrypt the encrypted file
  39. unsigned char* decryptedFile;
  40. int decryptedFileLength;
  41. if((decryptedFileLength = (dynamic_cast<decrypt*>(O2))->aesDecrypt(file, fileLength, &decryptedFile)) == -1) {
  42. fprintf(stderr, "Decryption failed\n");
  43. return 1;
  44. }
  45. printf("%d bytes decrypted\n", (int)decryptedFileLength);
  46. std::cerr << O2->PrintAesKey() << std::endl;
  47. // Write the decrypted file to its own file
  48. std::string decryptedFilename = O2->writeFile(decryptedFile, decryptedFileLength);
  49. printf("Decrypted file written to \"%s\"\n", decryptedFilename.c_str());
  50. free(decryptedFile);
  51. free(file);
  52. delete O2;
  53. delete O;
  54. return 0;
  55. }