Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <Windows.h>
- #include <wincrypt.h>
- #include <chrono>
- using namespace std;
- // вывод ошибок
- void ErrorHandling(string message)
- {
- DWORD dw = GetLastError();
- cout << "Error with: " << message << "\n";
- cout << "Error code: " << dw << "\n";
- }
- string CreateMD5(string input)
- {
- HCRYPTPROV hCryptProv;
- HCRYPTHASH hHash;
- HCRYPTHASH hDuplicatedHash;
- if (CryptAcquireContext(
- &hCryptProv,
- nullptr,
- nullptr,
- PROV_RSA_FULL,
- 0))
- {
- //cout << "CryptAcquireContext complete. \n";
- }
- else
- {
- ErrorHandling("Acquisition of context failed.");
- }
- // получить дескриптор хэш объекта
- if (CryptCreateHash(
- hCryptProv,
- CALG_MD5,
- 0,
- 0,
- &hHash))
- {
- //cout << "An empty hash object has been created. \n";
- }
- else
- {
- ErrorHandling("CryptBeginHash");
- }
- if (CryptHashData(
- hHash, reinterpret_cast<const BYTE*>(input.c_str()), input.size(),0
- ))
- {
- //cout << "CryptHashData executed successfully\n";
- }
- else
- {
- ErrorHandling("CryptHashData");
- }
- if (CryptDuplicateHash(
- hHash,
- nullptr,
- 0,
- &hDuplicatedHash
- ))
- {
- //cout << "The hash has been duplicated. \n";
- }
- else
- {
- ErrorHandling("CryptDuplicateHash");
- }
- BYTE* pbHash;
- DWORD dwHashLen;
- DWORD dwHashLenSize = sizeof(DWORD);
- if (CryptGetHashParam(
- hDuplicatedHash,
- HP_HASHSIZE,
- reinterpret_cast<BYTE*>(&dwHashLen),
- &dwHashLenSize,
- 0
- ))
- {
- //cout << "CryptGetHashParam HP_HASHSIZE executed successfully\n";
- }
- else
- {
- ErrorHandling("CryptGetHashParam failed to get size.");
- }
- if (!(pbHash = static_cast<BYTE*>(malloc(dwHashLen))))
- cout << "Allocation failed\n";
- string res{};
- if (CryptGetHashParam(
- hHash,
- HP_HASHVAL,
- pbHash,
- &dwHashLen,
- 0))
- {
- if (pbHash == nullptr)
- {
- cout << "system error\n";
- exit(1);
- }
- // первый вариант вывода
- //cout << "hash is: ";
- for (size_t i = 0; i!= 16; ++i)
- {
- res += "0123456789abcdef"[pbHash[i] / 16];
- res += "0123456789abcdef"[pbHash[i] % 16];
- }
- //cout << res << "\n";
- }
- else
- {
- ErrorHandling("CryptGetHashParam\n");
- }
- if (hHash)
- CryptDestroyHash(hHash);
- if (hDuplicatedHash)
- CryptDestroyHash(hDuplicatedHash);
- if (hCryptProv)
- CryptReleaseContext(hCryptProv, 0);
- return res;
- }
- int main()
- {
- string input = CreateMD5("9560");
- cout << "Bruteforcing " << input << " ...\n";
- auto start = std::chrono::high_resolution_clock::now();
- auto stop = start;
- auto found = false;
- for (int i = 0; i <= 9999; i++)
- {
- if (CreateMD5(to_string(i)) == input)
- {
- cout << "Found! " << i << " " << CreateMD5(to_string(i)) << "\n";
- found = true;
- stop = std::chrono::high_resolution_clock::now();
- std::cout << "Time: " << std::chrono::duration_cast<std::chrono::seconds>(stop - start).count()
- << " seconds\n";
- break;
- }
- }
- if (!found)
- cout << "Hash MD5 doesn't found!\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement