Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include "Key.h"
  4. #include <unordered_map>
  5. #include <vector>
  6. //#include <utility>
  7.  
  8. using namespace std;
  9.  
  10. namespace std
  11. {
  12. template<>
  13. struct hash<Key>{
  14. size_t operator()(const Key & k) const
  15. {
  16. size_t b{16777619};
  17. for(int i = 0; i < C; i++)
  18. {
  19. b =2166136261 *b^(k.digit[i]);
  20. }
  21. return b;
  22. }
  23. };
  24. }
  25.  
  26. int
  27. main(int argc, char* argv[])
  28. {
  29. unsigned char buffer[C+1]; // temporary string buffer
  30. Key encrypted; // the encrypted password
  31. Key zero = {{0}}; // the all zero key
  32. Key T[N]; // the table T
  33.  
  34.  
  35.  
  36. if (argc != 2)
  37. {
  38. cout << "Usage:" << endl << argv[0] << " password < rand8.txt" << endl;
  39.  
  40. return 1;
  41. }
  42.  
  43. encrypted = KEYinit((unsigned char *) argv[1]);
  44.  
  45. // read in table T
  46. for (int i{0}; i < N; ++i)
  47. {
  48. scanf("%s", buffer);
  49. T[i] = KEYinit(buffer);
  50. }
  51.  
  52. auto begin = chrono::high_resolution_clock::now();
  53.  
  54. Key half_it{{0}}; // Create a Key iterator with value 0
  55. half_it++; // add 1 to the iterator
  56. for (int i = 0; i <= N/2; ++i) { // for (Amount of bits of the password /2)
  57. half_it = half_it + half_it;
  58. }
  59.  
  60. unordered_map<Key, vector<Key>> table; // Create the map to save "low" values inside
  61.  
  62. Key low{{0}};
  63. do {
  64. table[encrypted - KEYsubsetsum(low, T)].push_back(low); // put low in the table
  65. ++low;
  66. } while(low <= half_it);
  67.  
  68. Key high{{0}}; // Kind of a Key iterator for the high part
  69. do {
  70. unordered_map<Key, vector<Key>>::iterator it = table.find(KEYsubsetsum(high, T)); // Create a iterator, pointing to the position of higherhalf
  71. if (it != table.end()) { // If the iterator is in the end there wasn't a match and it's not a password.
  72. for (Key candidate : it->second) { //If we are here we have found some password(s). So lets output all the passwords in the vector
  73. cout << high + candidate << endl;
  74. }
  75. }
  76. high = high + half_it;
  77. } while (high != zero);
  78.  
  79.  
  80. auto end = chrono::high_resolution_clock::now();
  81. cout << "Decryption took "
  82. << std::chrono::duration_cast<chrono::seconds>(end - begin).count()
  83. << " seconds." << endl;
  84.  
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement