Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <iomanip>
- #include <iostream>
- #include <sstream>
- #include <string>
- #ifndef CRYPTOPP_ENABLE_NAMESPACE_WEAK
- #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
- #endif
- #include <cryptopp\md5.h>
- #include <cryptopp\filters.h>
- #include <cryptopp\hex.h>
- #pragma comment(lib, "cryptopp\\cryptlib_debug_dll")
- uint8_t hash_table[256][256];
- void initialize_hash_table()
- {
- for (uint32_t i = 0; i < 256; i++)
- {
- for (uint32_t j = 0; j < 256; j++)
- {
- uint8_t key = 0;
- uint8_t column = i;
- uint8_t row = j;
- while (column != 0 && row != 0)
- {
- if (column & 1)
- key ^= row;
- if (row & 0x80)
- {
- row *= 2;
- row ^= 0x2B;
- }
- else
- {
- row *= 2;
- }
- column >>= 1;
- }
- hash_table[i][j] = key;
- }
- }
- }
- void generate_name_hash(std::string const& name, std::string& hash)
- {
- hash.clear();
- uint8_t digest[CryptoPP::Weak::MD5::DIGESTSIZE];
- CryptoPP::Weak::MD5 md5;
- md5.CalculateDigest(digest, reinterpret_cast<const uint8_t*>(name.c_str()), name.length());
- CryptoPP::HexEncoder encoder;
- encoder.Attach(new CryptoPP::StringSink(hash));
- encoder.Put(digest, sizeof(digest));
- encoder.MessageEnd();
- }
- void generate_name_checksum(std::string const& name, std::string& checksum)
- {
- checksum.clear();
- int32_t key = 0;
- for (std::size_t i = 0; i < name.length(); i++)
- {
- uint8_t next = static_cast<uint8_t>(i + 1);
- int32_t xor_key = next ^ (name.at(i & 0xFF) + key);
- int32_t xor_product = (xor_key ^ (next | 8)) + xor_key;
- key = std::abs(static_cast<int32_t>(std::pow(xor_product, 2))) % 0x1000;
- std::stringstream ss;
- ss << std::hex << std::uppercase << key;
- checksum.append(ss.str());
- }
- }
- void generate_serial_checksum(std::string const& serial, std::string& checksum)
- {
- checksum.clear();
- for (std::size_t i = 0, offset = 0; i < serial.length(); i++)
- {
- if (i == 0)
- checksum.push_back(static_cast<uint8_t>(std::stoul(serial.substr(i, 2), 0, 16)));
- else if (serial.at(i) == ':')
- checksum.push_back(static_cast<uint8_t>(std::stoul(serial.substr(i + 1, 2), 0, 16)));
- }
- }
- void generate_row_table(std::string const& name_checksum, uint8_t* row_table)
- {
- uint8_t a = 0, b = 0, c = 0;
- for (uint32_t i = 0; i < 64; i++)
- {
- for (uint32_t j = 0; j < name_checksum.length(); j++)
- {
- c = b ^ name_checksum.at(j);
- b = a ^ c ^ i ^ 2;
- a = hash_table[c][b];
- a ^= b ^ c ^ i;
- }
- row_table[i] = a ^ b ^ c;
- }
- }
- void generate_product_table(std::string const& serial_checksum, uint8_t* product_table)
- {
- product_table[5] = serial_checksum.at(0);
- product_table[9] = serial_checksum.at(1);
- product_table[1] = serial_checksum.at(2);
- product_table[3] = serial_checksum.at(3);
- product_table[15] = serial_checksum.at(4);
- product_table[2] = serial_checksum.at(5);
- product_table[8] = serial_checksum.at(6);
- product_table[13] = serial_checksum.at(7);
- product_table[4] = serial_checksum.at(8);
- product_table[11] = serial_checksum.at(9);
- product_table[6] = serial_checksum.at(10);
- product_table[14] = serial_checksum.at(11);
- product_table[7] = serial_checksum.at(12);
- product_table[10] = serial_checksum.at(13);
- product_table[0] = serial_checksum.at(14);
- product_table[12] = serial_checksum.at(15);
- product_table[26] = serial_checksum.at(16);
- product_table[25] = serial_checksum.at(17);
- product_table[17] = serial_checksum.at(18);
- product_table[19] = serial_checksum.at(19);
- product_table[31] = serial_checksum.at(20);
- product_table[18] = serial_checksum.at(21);
- product_table[24] = serial_checksum.at(22);
- product_table[20] = serial_checksum.at(23);
- product_table[29] = serial_checksum.at(24);
- product_table[27] = serial_checksum.at(25);
- product_table[30] = serial_checksum.at(26);
- product_table[22] = serial_checksum.at(27);
- product_table[23] = serial_checksum.at(28);
- product_table[21] = serial_checksum.at(29);
- product_table[16] = serial_checksum.at(30);
- product_table[28] = serial_checksum.at(31);
- }
- void generate_serial_hash(std::string const& serial_checksum, std::string const& name_checksum, std::string& hash)
- {
- hash.clear();
- uint8_t row_table[64];
- generate_row_table(name_checksum, row_table);
- uint8_t product_table[32];
- generate_product_table(serial_checksum, product_table);
- for (uint32_t i = 0; i < 64; i++)
- {
- uint8_t key_table[16];
- for (uint32_t j = 0; j < 16; j++)
- {
- uint32_t key = hash_table[(product_table[16 + j] * 2) & 0xFF][row_table[i & 63]];
- if (j == 0)
- key_table[j] = key;
- else
- {
- key = hash_table[key][key_table[j - 1]];
- key_table[j] = key_table[j - 1] ^ key;
- }
- }
- uint8_t shuffle_table[16];
- for (uint32_t j = 0; j < 16; j++)
- shuffle_table[j] = key_table[j] ^ product_table[j];
- memcpy(product_table, product_table + 16, 16);
- memcpy(product_table + 16, shuffle_table, 16);
- }
- hash.assign(product_table, product_table + 32);
- }
- void generate_reverse_product_table(uint8_t* product_table, std::string& serial_checksum)
- {
- serial_checksum.clear();
- serial_checksum.push_back(product_table[5]);
- serial_checksum.push_back(product_table[9]);
- serial_checksum.push_back(product_table[1]);
- serial_checksum.push_back(product_table[3]);
- serial_checksum.push_back(product_table[15]);
- serial_checksum.push_back(product_table[2]);
- serial_checksum.push_back(product_table[8]);
- serial_checksum.push_back(product_table[13]);
- serial_checksum.push_back(product_table[4]);
- serial_checksum.push_back(product_table[11]);
- serial_checksum.push_back(product_table[6]);
- serial_checksum.push_back(product_table[14]);
- serial_checksum.push_back(product_table[7]);
- serial_checksum.push_back(product_table[10]);
- serial_checksum.push_back(product_table[0]);
- serial_checksum.push_back(product_table[12]);
- serial_checksum.push_back(product_table[26]);
- serial_checksum.push_back(product_table[25]);
- serial_checksum.push_back(product_table[17]);
- serial_checksum.push_back(product_table[19]);
- serial_checksum.push_back(product_table[31]);
- serial_checksum.push_back(product_table[18]);
- serial_checksum.push_back(product_table[24]);
- serial_checksum.push_back(product_table[20]);
- serial_checksum.push_back(product_table[29]);
- serial_checksum.push_back(product_table[27]);
- serial_checksum.push_back(product_table[30]);
- serial_checksum.push_back(product_table[22]);
- serial_checksum.push_back(product_table[23]);
- serial_checksum.push_back(product_table[21]);
- serial_checksum.push_back(product_table[16]);
- serial_checksum.push_back(product_table[28]);
- }
- void generate_serial(std::string const& name_hash, std::string const& name_checksum, std::string& serial_number)
- {
- serial_number.clear();
- uint8_t row_table[64];
- generate_row_table(name_checksum, row_table);
- uint8_t product_table[32];
- memcpy(product_table, name_hash.data(), name_hash.size());
- for (uint32_t i = 64; i > 0; i--)
- {
- uint8_t shuffle_table[16];
- memcpy(shuffle_table, product_table + 16, 16);
- memcpy(product_table + 16, product_table, 16);
- uint8_t key_table[16];
- for (uint32_t j = 0; j < 16; j++)
- {
- uint32_t key = hash_table[(product_table[16 + j] * 2) & 0xFF][row_table[(i - 1) & 63]];
- if (j == 0)
- key_table[j] = key;
- else
- {
- key = hash_table[key][key_table[j - 1]];
- key_table[j] = key_table[j - 1] ^ key;
- }
- }
- for (uint32_t j = 0; j < 16; j++)
- shuffle_table[j] ^= key_table[j];
- memcpy(product_table, shuffle_table, 16);
- }
- std::string serial_checksum;
- generate_reverse_product_table(product_table, serial_checksum);
- for (uint32_t i = 0; i < serial_checksum.size(); i++)
- {
- std::stringstream ss;
- ss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << (static_cast<uint32_t>(serial_checksum.at(i)) & 0xFF);
- serial_number.append(ss.str());
- if ((i + 1) != serial_checksum.size())
- serial_number.push_back(':');
- }
- }
- bool evaluate_hashes(std::string const& serial_name_hash, std::string const& serial_hash)
- {
- uint32_t equals = 0;
- for (uint32_t i = 0; i < 32; i++)
- {
- if (serial_hash.length() > i &&
- serial_name_hash.length() > i &&
- serial_hash.at(i) == serial_name_hash.at(i))
- {
- equals++;
- }
- }
- return (equals == 32);
- }
- bool evaluate(std::string const& serial_name, std::string const& serial_number)
- {
- if (!serial_name.empty() && !serial_number.empty())
- {
- /* Name */
- std::string serial_name_hash;
- generate_name_hash(serial_name, serial_name_hash);
- std::cout << "Serial name hash:\t" << serial_name_hash << std::endl;
- std::string serial_name_checksum;
- generate_name_checksum(serial_name, serial_name_checksum);
- std::cout << "Serial name checksum:\t" << serial_name_checksum << std::endl;
- /* Serial */
- std::string serial_checksum;
- generate_serial_checksum(serial_number, serial_checksum);
- std::string serial_hash;
- generate_serial_hash(serial_checksum, serial_name_checksum, serial_hash);
- /* Evaluation */
- std::cout << std::endl;
- if (evaluate_hashes(serial_name_hash, serial_hash))
- return true;
- }
- return false;
- }
- void generate(std::string const& serial_name)
- {
- /* Name */
- std::string serial_name_hash;
- generate_name_hash(serial_name, serial_name_hash);
- std::string serial_name_checksum;
- generate_name_checksum(serial_name, serial_name_checksum);
- /* Serial */
- std::string serial_number;
- generate_serial(serial_name_hash, serial_name_checksum, serial_number);
- if (evaluate(serial_name, serial_number))
- std::cout << "Serial number:\t\t" << serial_number << std::endl;
- }
- void perform_test()
- {
- initialize_hash_table();
- std::string serial_name;
- std::cout << "Enter serial name:\t";
- std::getline(std::cin, serial_name);
- std::cout << std::endl;
- generate(serial_name);
- }
- #include <Windows.h>
- int main()
- {
- RECT desktop;
- SystemParametersInfo(SPI_GETWORKAREA, 0, &desktop, 0);
- MoveWindow(GetConsoleWindow(), 0, 0, 1000, (desktop.bottom - desktop.top), TRUE);
- perform_test();
- std::cin.ignore();
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement