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 6.1 kB

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

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