Advertisement
perjespersson

Decrypt

Sep 23rd, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include "key.h"
  4. #include <map>
  5. #include <vector>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. int main(int argc, char* argv[]) {
  11. if (argc != 2) {
  12. cout << "Usage:" << endl;
  13. cout << argv[0] << " <hashed password> < rand8.txt" << endl;
  14. return 1;
  15. }
  16.  
  17. // Hashed password.
  18. Key hashed{argv[1]};
  19. Key zero{};
  20. // Table.
  21. Key table[N];
  22.  
  23. // Unordered map containing combinations of floor of size of byte/2
  24. //map<size_t,Key> decrypt;
  25. map<Key, Key> decrypt;
  26.  
  27. // Read table.
  28. for (int i{0}; i < N; i++) {
  29. char buffer[C + 1];
  30. if (scanf("%s", buffer) != 1) {
  31. cerr << "Too short table!" << endl;
  32. return 1;
  33. }
  34. table[i] = Key{buffer};
  35. }
  36.  
  37. auto begin = chrono::high_resolution_clock::now();
  38.  
  39.  
  40. // Find all possible passwords that hash to 'hashed' and print them.
  41.  
  42. // Put all combinations of encrypted lower half in map decrypt.
  43. Key candidate{};
  44.  
  45. do {
  46. Key enc = subset_sum(candidate, table); //kryptera candidate
  47. decrypt[hashed-enc] = candidate;
  48. ++candidate;
  49. } while (!candidate.bit(N/2));
  50.  
  51. Key higher_half{candidate};
  52. // higher_half= candidate;
  53.  
  54. do{
  55. auto iterator = decrypt.find(subset_sum(higher_half, table));
  56. if (iterator != decrypt.end())
  57. cout << higher_half + iterator->second << endl;
  58. higher_half+= candidate;
  59. } while(higher_half != zero );
  60.  
  61.  
  62.  
  63.  
  64. auto end = chrono::high_resolution_clock::now();
  65. cout << "Decryption took "
  66. << std::chrono::duration_cast<chrono::seconds>(end - begin).count()
  67. << " seconds." << endl;
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement