You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

rc4_encrypt.cpp 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <memory>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <vector>
  7. #include "../src/decryption/rc4/rc4_cryption_base.h"
  8. #include "../src/decryption/rc4_cryption.h"
  9. using namespace lite;
  10. std::shared_ptr<void> read_file(std::string file_path, size_t& size) {
  11. FILE* fin = fopen(file_path.c_str(), "rb");
  12. if (!fin) {
  13. printf("failed to open %s.", file_path.c_str());
  14. };
  15. fseek(fin, 0, SEEK_END);
  16. size = ftell(fin);
  17. fseek(fin, 0, SEEK_SET);
  18. void* ptr = malloc(size);
  19. std::shared_ptr<void> buf{ptr, ::free};
  20. fread(buf.get(), 1, size, fin);
  21. fclose(fin);
  22. return buf;
  23. }
  24. void write_file(std::string file_path, const std::vector<uint8_t>& data) {
  25. FILE* fin = fopen(file_path.c_str(), "wb");
  26. if (!fin) {
  27. printf("failed to open %s.", file_path.c_str());
  28. };
  29. fwrite(data.data(), 1, data.size(), fin);
  30. fclose(fin);
  31. }
  32. typedef int (*CommandHandler)(int, char**);
  33. const char* usage =
  34. "Usage:\n"
  35. " rc4_encryptor encrypt_predefined_rc4 <input file> <output file>\n"
  36. " rc4_encryptor encrypt_rc4 <hash key> <enc key> <input file> <output "
  37. "file>\n"
  38. " rc4_encryptor encrypt_predefined_sfrc4 <input file> <output file>\n"
  39. " rc4_encryptor encrypt_sfrc4 <hash key> <enc key> <input file> "
  40. "<output "
  41. "file>\n"
  42. " rc4_encryptor hash <input file>\n";
  43. int command_encrypt_predefined_rc4(int argc, char** argv) {
  44. if (argc != 4) {
  45. printf("Invalid encrypt_predefined_rc4 arguments.\n");
  46. return 1;
  47. }
  48. const char* input_file_path = argv[2];
  49. const char* output_file_path = argv[3];
  50. size_t size = 0;
  51. auto keys = RC4::get_decrypt_key();
  52. auto input = read_file(input_file_path, size);
  53. printf("Reading input file ...\n");
  54. auto output = RC4::encrypt_model(input.get(), size, keys);
  55. write_file(output_file_path, output);
  56. printf("Done.\n");
  57. return 0;
  58. }
  59. int command_encrypt_rc4(int argc, char** argv) {
  60. if (argc != 6) {
  61. printf("Invalid encrypt_rc4 arguments.\n");
  62. return 1;
  63. }
  64. uint64_t hash_key = std::stoull(argv[2], 0, 0);
  65. uint64_t enc_key = std::stoull(argv[3], 0, 0);
  66. const char* input_file_path = argv[4];
  67. const char* output_file_path = argv[5];
  68. std::vector<uint8_t> keys(128, 0);
  69. uint64_t* data = reinterpret_cast<uint64_t*>(keys.data());
  70. data[0] = hash_key;
  71. data[1] = enc_key;
  72. size_t size = 0;
  73. auto input = read_file(input_file_path, size);
  74. printf("Reading input file ...\n");
  75. auto output = RC4::encrypt_model(input.get(), size, keys);
  76. printf("Encrypting ...\n");
  77. write_file(output_file_path, output);
  78. printf("Done.\n");
  79. return 0;
  80. }
  81. int command_encrypt_predefined_sfrc4(int argc, char** argv) {
  82. if (argc != 4) {
  83. printf("Invalid encrypt_predefined_rc4 arguments.\n");
  84. return 1;
  85. }
  86. const char* input_file_path = argv[2];
  87. const char* output_file_path = argv[3];
  88. size_t size = 0;
  89. auto keys = SimpleFastRC4::get_decrypt_key();
  90. auto input = read_file(input_file_path, size);
  91. printf("Reading input file ...\n");
  92. auto output = SimpleFastRC4::encrypt_model(input.get(), size, keys);
  93. write_file(output_file_path, output);
  94. printf("Done.\n");
  95. return 0;
  96. }
  97. int command_encrypt_sfrc4(int argc, char** argv) {
  98. if (argc != 6) {
  99. printf("Invalid encrypt_rc4 arguments.\n");
  100. return 1;
  101. }
  102. uint64_t hash_key = std::stoull(argv[2], 0, 0);
  103. uint64_t enc_key = std::stoull(argv[3], 0, 0);
  104. const char* input_file_path = argv[4];
  105. const char* output_file_path = argv[5];
  106. std::vector<uint8_t> keys(128, 0);
  107. uint64_t* data = reinterpret_cast<uint64_t*>(keys.data());
  108. data[0] = hash_key;
  109. data[1] = enc_key;
  110. size_t size = 0;
  111. auto input = read_file(input_file_path, size);
  112. printf("Reading input file ...\n");
  113. auto output = SimpleFastRC4::encrypt_model(input.get(), size, keys);
  114. printf("Encrypting ...\n");
  115. write_file(output_file_path, output);
  116. printf("Done.\n");
  117. return 0;
  118. }
  119. int command_hash(int argc, char** argv) {
  120. if (argc != 3) {
  121. printf("Invalid hash arguments.\n");
  122. return 1;
  123. }
  124. const char* input_file_path = argv[2];
  125. size_t len = 0;
  126. auto input = read_file(input_file_path, len);
  127. rc4::FastHash64 hasher(rc4::key_gen_hash_key());
  128. auto start = static_cast<const char*>(input.get());
  129. auto ptr = reinterpret_cast<const uint64_t*>(start);
  130. while (reinterpret_cast<const char*>(ptr + 1) <= start + len) {
  131. hasher.feed(*ptr);
  132. ++ptr;
  133. }
  134. auto cptr = reinterpret_cast<const char*>(ptr);
  135. if (cptr < start + len) {
  136. uint64_t v = 0;
  137. std::copy(cptr, start + len, reinterpret_cast<char*>(&v));
  138. hasher.feed(v);
  139. }
  140. printf("%llx\n", static_cast<unsigned long long>(hasher.get()));
  141. return 0;
  142. }
  143. std::unordered_map<std::string, CommandHandler> commands = {
  144. {"encrypt_predefined_rc4", command_encrypt_predefined_rc4},
  145. {"encrypt_rc4", command_encrypt_rc4},
  146. {"encrypt_predefined_sfrc4", command_encrypt_predefined_sfrc4},
  147. {"encrypt_sfrc4", command_encrypt_sfrc4},
  148. {"hash", command_hash},
  149. };
  150. int main(int argc, char** argv) {
  151. if (argc == 1) {
  152. printf("%s", usage);
  153. return 1;
  154. }
  155. auto it = commands.find(argv[1]);
  156. if (it == commands.end()) {
  157. printf("Invalid command arguments.\n");
  158. printf("%s", usage);
  159. return 1;
  160. }
  161. return it->second(argc, argv);
  162. }
  163. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}