decipher.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cstring>
  4. #include "cipher/aes.h"
  5. #include "cipher/des.h"
  6. #include "cipher/des3.h"
  7. #include "cipher/rc4.h"
  8. #include "cipher/rsa.h"
  9. #include "cipher/ecc.h"
  10. #include "lzo/minilzo.h"
  11. #include "ucl/ucl.h"
  12. #include "zstd/zstd.h"
  13. #include "lz4/lz4.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include "lzma/FileIO.h"
  18. #include "lzma/TinyLzmaCompress.h"
  19. #include "lzma/TinyLzmaDecompress.h"
  20. #ifdef __cplusplus
  21. }
  22. #endif
  23. #define HEAP_ALLOC(var, size) \
  24. lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
  25. // 定义各个字段的偏移量
  26. #define SYMMETRIC_ENC_TYP_OFFSET 0
  27. #define ASYMMETRIC_ENC_TYP_OFFSET 1
  28. #define COMP_TYP_OFFSET 2
  29. #define INITIAL_FILE_SIZE_OFFSET 3
  30. #define COMPRESSED_FILE_SIZE_OFFSET 7
  31. #define ENCRYPTED_FILE_SIZE_OFFSET 11
  32. #define ENCRYPTED_KEY_SIZE_OFFSET 15
  33. #define PROTECT_TYPE_OFFSET 19
  34. #define MAC_NUMS_OFFSET 20
  35. static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
  36. const int MAGIC_LENGTH = 32;
  37. int deenc(char *f) {
  38. uint8_t *magic = (uint8_t *) malloc(sizeof(uint8_t) * MAGIC_LENGTH);
  39. FILE *fp = fopen(f, "rb");
  40. size_t magic_read_count = fread(magic, sizeof(uint8_t), MAGIC_LENGTH, fp);
  41. if (magic_read_count != MAGIC_LENGTH) {
  42. printf("error: Unable to read %d bytes from file\n", MAGIC_LENGTH);
  43. return EXIT_FAILURE;
  44. }
  45. uint8_t symmetric_enc_typ = magic[SYMMETRIC_ENC_TYP_OFFSET];
  46. uint8_t asymmetric_enc_typ = magic[ASYMMETRIC_ENC_TYP_OFFSET];
  47. uint8_t comp_typ = magic[COMP_TYP_OFFSET];
  48. uint32_t initial_file_size;
  49. memcpy(&initial_file_size, magic + INITIAL_FILE_SIZE_OFFSET, sizeof(uint32_t));
  50. uint32_t compressed_file_size;
  51. memcpy(&compressed_file_size, magic + COMPRESSED_FILE_SIZE_OFFSET, sizeof(uint32_t));
  52. uint32_t encrypted_file_size;
  53. memcpy(&encrypted_file_size, magic + ENCRYPTED_FILE_SIZE_OFFSET, sizeof(uint32_t));
  54. uint32_t encrypted_key_size;
  55. memcpy(&encrypted_key_size, magic + ENCRYPTED_KEY_SIZE_OFFSET, sizeof(uint32_t));
  56. uint8_t protect_type;
  57. memcpy(&protect_type, magic + PROTECT_TYPE_OFFSET, sizeof(uint8_t));
  58. uint8_t mac_num;
  59. memcpy(&mac_num, magic + MAC_NUMS_OFFSET, sizeof(uint8_t));
  60. /**
  61. * DES: 64bit 1 key_length 8
  62. * AES: 128bit 2 key_length 32
  63. * 3DES: 64bit 3 key_length 24
  64. * RC4: 256bit 4 key_length 32
  65. */
  66. uint8_t input_length, key_length;
  67. if (symmetric_enc_typ == 1) input_length = 8, key_length = 8;
  68. else if (symmetric_enc_typ == 2) input_length = 16, key_length = 32;
  69. else if (symmetric_enc_typ == 3) input_length = 8, key_length = 24;
  70. else if (symmetric_enc_typ == 4) input_length = 1, key_length = 32;
  71. else input_length = 0, key_length = 0;
  72. printf("uint8_t symmetric_enc_typ: %u\n", symmetric_enc_typ);
  73. printf("uint8_t asymmetric_enc_typ: %u\n", asymmetric_enc_typ);
  74. printf("uint8_t comp_typ: %u\n", comp_typ);
  75. printf("uint32_t file_size: %u\n", initial_file_size);
  76. printf("uint32_t compressed_file_size: %u\n", compressed_file_size);
  77. printf("uint32_t encrypted_file_size: %u\n", encrypted_file_size);
  78. printf("uint8_t input_length: %u\n", input_length);
  79. printf("uint8_t key_length: %u\n", key_length);
  80. auto *encrypted_array = (uint8_t *) malloc(sizeof(uint8_t) * encrypted_file_size);
  81. // auto *shuffle = (uint8_t *) malloc(sizeof(uint8_t) * key_length);
  82. uint8_t *encrypted_key = (uint8_t *) malloc(encrypted_key_size);
  83. printf("encrypted_array confused_key malloc success\n");
  84. size_t encrypted_read_count = fread(encrypted_array, sizeof(uint8_t), encrypted_file_size, fp);
  85. if (encrypted_read_count != encrypted_file_size) {
  86. printf("error: unable to read %d bytes from file\n", encrypted_file_size);
  87. return EXIT_FAILURE;
  88. }
  89. struct public_key_class pub[1];
  90. struct private_key_class priv[1];
  91. uint8_t pub_key[ECC_KEYSIZE];
  92. uint8_t prv_key[ECC_KEYSIZE];
  93. if (asymmetric_enc_typ != 0) {
  94. size_t key_read_count = fread(encrypted_key, sizeof(uint8_t), encrypted_key_size, fp);
  95. if (key_read_count != encrypted_key_size) {
  96. printf("error: unable to read %d bytes from file\n", encrypted_key_size);
  97. return EXIT_FAILURE;
  98. }
  99. if (asymmetric_enc_typ == 5) {
  100. size_t pub_bytes_read = fread(&pub, sizeof(struct public_key_class), 1, fp);
  101. if (pub_bytes_read != 1) {
  102. printf("error: unable to read %d bytes from file\n", 1);
  103. return EXIT_FAILURE;
  104. }
  105. size_t priv_bytes_read = fread(&priv, sizeof(struct private_key_class), 1, fp);
  106. if (priv_bytes_read != 1) {
  107. printf("error: unable to read %d bytes from file\n", 1);
  108. return EXIT_FAILURE;
  109. }
  110. /**
  111. * Private Key:
  112. Modulus: 3463516351
  113. Exponent: 2429436001
  114. Public Key:
  115. Modulus: 3463516351
  116. Exponent: 131073
  117. *
  118. */
  119. printf("Private Key:\n Modulus: %lld\n Exponent: %lld\n", (long long) priv->modulus, (long long) priv->exponent);
  120. printf("Public Key:\n Modulus: %lld\n Exponent: %lld\n", (long long) pub->modulus, (long long) pub->exponent);
  121. /**
  122. * 1384984386 1683296021 2114995281 834109981 872949891 832896308 1564298768 1546948745
  123. */
  124. } else if (asymmetric_enc_typ == 6) {
  125. size_t pub_bytes_read = fread(&pub_key, sizeof(uint8_t), ECC_KEYSIZE, fp);
  126. if (pub_bytes_read != ECC_KEYSIZE) {
  127. printf("error: unable to read %d bytes from file\n", ECC_KEYSIZE);
  128. return EXIT_FAILURE;
  129. }
  130. size_t priv_bytes_read = fread(&prv_key, sizeof(uint8_t), ECC_KEYSIZE, fp);
  131. if (priv_bytes_read != ECC_KEYSIZE) {
  132. printf("error: unable to read %d bytes from file\n", ECC_KEYSIZE);
  133. return EXIT_FAILURE;
  134. }
  135. printf("ECC prv_key:\n");
  136. for (int i = 0; i < ECC_KEYSIZE; i++) {
  137. printf("0x%02x,", prv_key[i]);
  138. }
  139. printf("\n");
  140. }
  141. }
  142. printf("mac info\n");
  143. uint8_t mac_array[10][18];
  144. for (int i = 0; i < mac_num; i++) {
  145. fread(mac_array[i], sizeof(uint8_t), 18, fp);
  146. printf("%s\n", mac_array[i]);
  147. }
  148. uint8_t *confused_key = (uint8_t *) (encrypted_key);
  149. printf("enc key:\n");
  150. for (int i = 0; i < encrypted_key_size; i++) {
  151. printf("%02x ", confused_key[i]);
  152. }
  153. printf("\n");
  154. if (asymmetric_enc_typ != 0) {
  155. if (asymmetric_enc_typ == 5) {
  156. printf("start to decrypt key using RSA\n");
  157. confused_key = rsa_decrypt((long long *) confused_key, encrypted_key_size, priv);
  158. if (confused_key == NULL) {
  159. printf("failed to decrypt using RSA\n");
  160. return EXIT_FAILURE;
  161. }
  162. } else if (asymmetric_enc_typ == 6) {
  163. printf("start to decrypt key using ECC\n");
  164. uint32_t tm_encrypted_key_size;
  165. ecc_decrypt(prv_key, (uint8_t *) encrypted_key, encrypted_key_size, confused_key, &tm_encrypted_key_size);
  166. if (encrypted_key_size != tm_encrypted_key_size) {
  167. printf("failed to decrypt using ECC\n");
  168. return EXIT_FAILURE;
  169. }
  170. }
  171. }
  172. printf("decrypted challenge data or key:\n");
  173. for (int i = 0; i < key_length; i++) {
  174. printf("%02x ", confused_key[i]);
  175. }
  176. printf("\n");
  177. if (protect_type == 1) {
  178. //通信获取秘钥
  179. uint8_t puf_value[39] = {0xa5, 0x5a, 0x20, 0x00, 0x8f, 0xad, 0x85, 0xc8, 0xc1, 0xc2, 0x85, 0xbc, 0x4c, 0x22, 0x9d,
  180. 0xa2, 0x5e, 0x6e, 0x44, 0x4c, 0xf5, 0x65, 0xfb, 0x8c, 0x4b, 0xb3, 0x4f, 0xc0, 0xd9, 0x02,
  181. 0xa0, 0xaa, 0xb9, 0xb6, 0x0a, 0xb4, 0x08, 0x6c, 0xff};
  182. memcpy(confused_key, puf_value + 4, 32);
  183. }
  184. uint8_t *decryption_array = (uint8_t *) malloc(compressed_file_size * sizeof(uint8_t));
  185. uint8_t *initial_file_array = (uint8_t *) malloc(initial_file_size * sizeof(uint8_t));
  186. printf("get key successfully\n");
  187. printf("final bytes:\n");
  188. for (int i = 0; i < 8; i++) {
  189. printf("%02x ", encrypted_array[i]);
  190. }
  191. printf("\n");
  192. for (int i = encrypted_file_size - 8; i < encrypted_file_size; i++) {
  193. printf("%02x ", encrypted_array[i]);
  194. }
  195. printf("\n");
  196. if (symmetric_enc_typ > 0 && comp_typ > 0) {
  197. // compress -> cryption
  198. // decryption
  199. printf("decryption_array malloc success\n");
  200. if (symmetric_enc_typ == 1) {
  201. printf("start decrypt with DES\n");
  202. DesContext des_context;
  203. desInit(&des_context, confused_key, key_length);
  204. for (int i = 0; i < encrypted_file_size; i += input_length) {
  205. desDecryptBlock(&des_context, encrypted_array + i, decryption_array + i);
  206. }
  207. printf("decryption successes with DES\n");
  208. } else if (symmetric_enc_typ == 2) {
  209. printf("start decrypt with AES\n");
  210. AesContext aes_context;
  211. aesInit(&aes_context, confused_key, key_length);
  212. for (int i = 0; i < encrypted_file_size; i += input_length) {
  213. aesDecryptBlock(&aes_context, encrypted_array + i, decryption_array + i);
  214. }
  215. printf("decryption successes with AES\n");
  216. } else if (symmetric_enc_typ == 3) {
  217. printf("start decrypt with 3DES\n");
  218. Des3Context des3_context;
  219. des3Init(&des3_context, confused_key, key_length);
  220. for (int i = 0; i < encrypted_file_size; i += input_length) {
  221. des3DecryptBlock(&des3_context, encrypted_array + i, decryption_array + i);
  222. }
  223. printf("decryption successes with TDEA\n");
  224. } else if (symmetric_enc_typ == 4) {
  225. printf("start decrypt with RC4\n");
  226. rc4_decrypt(encrypted_array, encrypted_file_size, confused_key, key_length);
  227. memcpy(decryption_array, encrypted_array, encrypted_file_size);
  228. printf("decryption successes with RC4\n");
  229. }
  230. printf("decrypt success\n");
  231. printf("compress bytes:\n");
  232. for (int i = 0; i < 8; i++) {
  233. printf("%02x ", decryption_array[i]);
  234. }
  235. printf("\n");
  236. // decompress
  237. printf("initial_file_array malloc success\n");
  238. int r = -1;
  239. unsigned long decompressed_file_size;
  240. if (comp_typ == 1) {
  241. printf("decompress with lzo...\n");
  242. r = lzo1x_decompress(decryption_array, compressed_file_size, initial_file_array, &decompressed_file_size, NULL);
  243. printf("%d\n", r);
  244. } else if (comp_typ == 2) {
  245. printf("decompress with ucl...\n");
  246. r = ucl_nrv2b_decompress_8(decryption_array, compressed_file_size, initial_file_array,
  247. reinterpret_cast<ucl_uint *>(&decompressed_file_size), NULL);
  248. printf("%d\n", r);
  249. decompressed_file_size = reinterpret_cast<unsigned long>(decompressed_file_size);
  250. } else if (comp_typ == 3) {
  251. printf("decompress with lzma...\n");
  252. r = tinyLzmaDecompress(decryption_array, compressed_file_size, initial_file_array, &decompressed_file_size);
  253. } else if (comp_typ == 4) {
  254. printf("decompress with zstd...\n");
  255. decompressed_file_size = ZSTD_decompress(initial_file_array, initial_file_size, decryption_array,
  256. compressed_file_size);
  257. printf("zstd %d\n", decompressed_file_size);
  258. } else if (comp_typ == 5) {
  259. printf("decompress with lz4...\n");
  260. decompressed_file_size = LZ4_decompress_safe(decryption_array, initial_file_array, compressed_file_size,
  261. initial_file_size);
  262. printf("LZ4 %d\n", decompressed_file_size);
  263. decompressed_file_size = reinterpret_cast<unsigned long>(decompressed_file_size);
  264. }
  265. printf("decompressed_file_size %u\n", decompressed_file_size);
  266. if (static_cast<unsigned int>(decompressed_file_size) == initial_file_size) {
  267. printf("decompressed %u bytes back into %u bytes\n",
  268. compressed_file_size, decompressed_file_size);
  269. } else {
  270. /* this should NEVER happen */
  271. printf("internal error - decompression failed: %d\n", r);
  272. return 1;
  273. }
  274. } else if (symmetric_enc_typ > 0) {
  275. }
  276. int length = strlen("depack_") + strlen(f) + 1;
  277. char result[length];
  278. memset(result, 0, sizeof(result));
  279. // 拼接字符串
  280. strcat(result, "depack_");
  281. strcat(result, f);
  282. printf("拼接结果: %s\n", result);
  283. FILE *p = fopen(result, "wb");
  284. fwrite(initial_file_array, sizeof(uint8_t), initial_file_size, p);
  285. fclose(p);
  286. return 0;
  287. }
  288. long get_file_size(const char *filename) {
  289. FILE *file = fopen(filename, "rb");
  290. if (file == NULL) {
  291. return -1;
  292. }
  293. fseek(file, 0, SEEK_END);
  294. long size = ftell(file);
  295. fclose(file);
  296. return size;
  297. }
  298. void cmp(char *f1, char *f2) {
  299. FILE *file;
  300. file = fopen("cmp.txt", "a");
  301. if (file == NULL) {
  302. printf("无法打开文件\n");
  303. return;
  304. }
  305. if (freopen("cmp.txt", "a", stdout) == NULL) {
  306. printf("无法重定向标准输出\n");
  307. return;
  308. }
  309. printf("%s cmp %s ", f1, f2);
  310. FILE *file1 = fopen(f1, "rb");
  311. FILE *file2 = fopen(f2, "rb");
  312. if (file1 == NULL || file2 == NULL) {
  313. printf("Error opening files.\n");
  314. return;
  315. }
  316. long size1 = get_file_size(f1);
  317. long size2 = get_file_size(f2);
  318. if (size1 != size2) {
  319. printf("File sizes are different.\n");
  320. return;
  321. }
  322. int isSame = 1;
  323. int byte1, byte2;
  324. while ((byte1 = fgetc(file1)) != EOF && (byte2 = fgetc(file2)) != EOF) {
  325. if (byte1 != byte2) {
  326. isSame = 0;
  327. break;
  328. }
  329. }
  330. fclose(file1);
  331. fclose(file2);
  332. if (isSame) printf("成功\n");
  333. else printf("失败\n");
  334. fclose(stdout);
  335. fclose(file);
  336. }
  337. int main(int argc, char *argv[]) {
  338. int i = 1, j = 5, k = 5;
  339. char f1[9];
  340. // 将 h, i, j, k 格式化写入 result 数组中
  341. sprintf(f1, "%c%d%d%d.bin", 't', i, j, k);
  342. deenc(f1);
  343. char f2[9 + 6];
  344. strcat(f2, "depack_");
  345. strcat(f2, f1);
  346. cmp("test.bin", f2);
  347. /**
  348. *
  349. * compress bytes:
  350. 03 43 00 00 ea 00 00 38
  351. 7f 01 00 00 00 11 00 00
  352. compress bytes:
  353. 62 23 3f 25 0b af 22 d1
  354. f4 23 94 2f de 0a 1b a5
  355. *
  356. *
  357. *
  358. * 151 main
  359. * compress bytes:
  360. 03 43 00 00 ea 00 00 38
  361. 7f 01 00 00 00 11 00 00
  362. compress bytes:
  363. 03 43 00 00 ea 00 00 38
  364. 7f 01 00 00 00 11 00 00
  365. main
  366. key:
  367. 6f ec 0f 26 70 ee 07 8e e7 ca 61 5b d9 2a 80 7d 17 67 fa c6 f0 b0 03 31 fc 7a 93 8e d6 a1 f1 fa
  368. 00 ec 00 00 70 00 00 00 e7 ca 00 00 00 00 80 00 51 00 00 c6 00 00 00 00 fc 00 00 00 00 00 f1 00
  369. *
  370. */
  371. }