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.9 kB

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

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台