wmain.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdio.h>
  2. #include <functional>
  3. #include "exception.hpp"
  4. #include "exceptions/operation_canceled_exception.hpp"
  5. #include "cp_converter.hpp"
  6. #include "base64_rfc4648.hpp"
  7. #include "navicat_serial_generator.hpp"
  8. #include "rsa_cipher.hpp"
  9. #define NKG_CURRENT_SOURCE_FILE() u8".\\navicat-keygen\\wmain.cpp"
  10. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  11. namespace nkg {
  12. using fnCollectInformation = std::function<navicat_serial_generator()>;
  13. using fnGenerateLicense = std::function<void(const rsa_cipher& cipher, const navicat_serial_generator& generator)>;
  14. navicat_serial_generator CollectInformationNormal();
  15. navicat_serial_generator CollectInformationAdvanced();
  16. void GenerateLicenseText(const rsa_cipher& cipher, const navicat_serial_generator& sn_generator);
  17. void GenerateLicenseBinary(const rsa_cipher& cipher, const navicat_serial_generator& sn_generator);
  18. }
  19. static void welcome() {
  20. _putws(L"***************************************************");
  21. _putws(L"* navicat-keygen by @DoubleLabyrinth *");
  22. _putws(L"* version: 16.0.7.0 *");
  23. _putws(L"***************************************************");
  24. _putws(L"");
  25. }
  26. static void help() {
  27. _putws(L"Usage:");
  28. _putws(L" navicat-keygen.exe <-bin|-text> [-adv] <RSA-2048 Private Key File>");
  29. _putws(L"");
  30. _putws(L" <-bin|-text> Specify \"-bin\" to generate \"license_file\" used by Navicat 11.");
  31. _putws(L" Specify \"-text\" to generate base64-encoded activation code.");
  32. _putws(L" This parameter is mandatory.");
  33. _putws(L"");
  34. _putws(L" [-adv] Enable advance mode.");
  35. _putws(L" This parameter is optional.");
  36. _putws(L"");
  37. _putws(L" <RSA-2048 Private Key File> A path to an RSA-2048 private key file.");
  38. _putws(L" This parameter is mandatory.");
  39. _putws(L"");
  40. _putws(L"Example:");
  41. _putws(L" navicat-keygen.exe -text .\\RegPrivateKey.pem");
  42. }
  43. int wmain(int argc, wchar_t* argv[]) {
  44. welcome();
  45. if (argc == 3 || argc == 4) {
  46. nkg::fnCollectInformation lpfnCollectInformation;
  47. nkg::fnGenerateLicense lpfnGenerateLicense;
  48. if (_wcsicmp(argv[1], L"-bin") == 0) {
  49. lpfnGenerateLicense = nkg::GenerateLicenseBinary;
  50. } else if (_wcsicmp(argv[1], L"-text") == 0) {
  51. lpfnGenerateLicense = nkg::GenerateLicenseText;
  52. } else {
  53. help();
  54. return -1;
  55. }
  56. if (argc == 3) {
  57. lpfnCollectInformation = nkg::CollectInformationNormal;
  58. } else if (argc == 4 && _wcsicmp(argv[2], L"-adv") == 0) {
  59. lpfnCollectInformation = nkg::CollectInformationAdvanced;
  60. } else {
  61. help();
  62. return -1;
  63. }
  64. try {
  65. nkg::rsa_cipher cipher;
  66. cipher.import_private_key_file(nkg::cp_converter<-1, CP_UTF8>::convert(argv[argc - 1]));
  67. if (cipher.bits() != 2048) {
  68. throw nkg::exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"RSA key length != 2048 bits.")
  69. .push_hint(u8"You must provide an RSA key whose modulus length is 2048 bits.");
  70. }
  71. auto sn_generator = lpfnCollectInformation();
  72. sn_generator.generate();
  73. _putws(L"[*] Serial number:");
  74. _putws(nkg::cp_converter<CP_UTF8, -1>::convert(sn_generator.serial_number_formatted()).c_str());
  75. _putws(L"");
  76. lpfnGenerateLicense(cipher, sn_generator);
  77. return 0;
  78. } catch (nkg::exceptions::operation_canceled_exception&) {
  79. return -1;
  80. } catch (nkg::exception& e) {
  81. wprintf_s(L"[-] %s:%d ->\n", nkg::cp_converter<CP_UTF8, -1>::convert(e.source_file()).c_str(), e.source_line());
  82. wprintf_s(L" %s\n", nkg::cp_converter<CP_UTF8, -1>::convert(e.custom_message()).c_str());
  83. if (e.error_code_exists()) {
  84. wprintf_s(L" %s (0x%zx)\n", nkg::cp_converter<CP_UTF8, -1>::convert(e.error_string()).c_str(), e.error_code());
  85. }
  86. for (auto& hint : e.hints()) {
  87. wprintf_s(L" Hints: %s\n", nkg::cp_converter<CP_UTF8, -1>::convert(hint).c_str());
  88. }
  89. return -1;
  90. } catch (std::exception& e) {
  91. wprintf_s(L"[-] %s\n", nkg::cp_converter<CP_UTF8, -1>::convert(e.what()).c_str());
  92. return -1;
  93. }
  94. } else {
  95. help();
  96. return -1;
  97. }
  98. }
  99. #undef NKG_CURRENT_SOURCE_FILE
  100. #undef NKG_CURRENT_SOURCE_LINE