From 12ecdd71592eccf7bdb6214edbc7318246469c1c Mon Sep 17 00:00:00 2001 From: yzrh Date: Fri, 14 Aug 2020 22:04:26 +0000 Subject: Initial commit. --- src/cnki_kdh.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/cnki_kdh.c (limited to 'src/cnki_kdh.c') diff --git a/src/cnki_kdh.c b/src/cnki_kdh.c new file mode 100644 index 0000000..8441319 --- /dev/null +++ b/src/cnki_kdh.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020, yzrh + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "cnki.h" + +int +cnki_kdh(cnki_t **param) +{ + if (*param == NULL) + return 1; + + if ((*param)->stat > 0) + printf("Begin 'KDH' decryption\n"); + + fseek((*param)->fp_i, 0, SEEK_END); + + long size = ftell((*param)->fp_i); + + fseek((*param)->fp_i, ADDRESS_KDH_BODY, SEEK_SET); + + const char key[] = KEY_KDH; + const int key_len = KEY_KDH_LENGTH; + long key_cur = 0; + + char buf[(*param)->size_buf]; + + for (;;) { + fread(buf, (*param)->size_buf, 1, (*param)->fp_i); + + for (int i = 0; i < (*param)->size_buf; i++) { + buf[i] ^= key[key_cur % key_len]; + key_cur++; + } + + fwrite(buf, (*param)->size_buf, 1, (*param)->fp_o); + + if (ftell((*param)->fp_i) == size) + break; + } + + if ((*param)->stat > 0) + printf("Decryption ended total %ld byte(s) written\n", + ftell((*param)->fp_o)); + + return 0; +} -- cgit v1.2.3