From 658f63031791a1cae36113ae6c04eeca3a9c51ac Mon Sep 17 00:00:00 2001 From: shenzexi <2538927534@qq.com> Date: Mon, 16 Dec 2024 20:42:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Reptile/kernel/encrypt/encrypt.c | 102 ++++++++++++++++----------- src/Reptile/reptile.code-workspace | 12 ++++ 2 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 src/Reptile/reptile.code-workspace diff --git a/src/Reptile/kernel/encrypt/encrypt.c b/src/Reptile/kernel/encrypt/encrypt.c index dd01ead..76fa232 100644 --- a/src/Reptile/kernel/encrypt/encrypt.c +++ b/src/Reptile/kernel/encrypt/encrypt.c @@ -4,49 +4,71 @@ #include "encrypt.h" +// 函数声明:获取文件大小 +// 参数:指向文件的指针 +// 返回值:文件的大小(字节数) static long get_file_size(FILE *file) { - long size; - fseek(file, 0, SEEK_END); - size = ftell(file); - rewind(file); - return size; + long size; + // 将文件指针移动到文件末尾 + fseek(file, 0, SEEK_END); + // 获取文件指针当前位置,即文件大小 + size = ftell(file); + // 将文件指针重新移动到文件开头 + rewind(file); + return size; } +// 主函数:程序入口 +// 参数:命令行参数个数,命令行参数数组 +// 返回值:程序退出状态码 int main(int argc, char **argv) { - if (argc != 3) { - fprintf(stderr, "USAGE: encrypt \n"); - exit(-1); - } - - FILE *file = fopen(argv[1], "rb"); - if (!file) { - fprintf(stderr, "Can't open %s for reading\n", argv[1]); - exit(-1); - } - - long size = get_file_size(file); - unsigned char *data = malloc(size); - if (!data) { - fprintf(stderr, "Can't allocate memory\n"); - exit(-1); - } - - if (fread(data, size, 1, file) != 1) { - fprintf(stderr, "Can't read data from file\n"); - exit(-1); - } - - fclose(file); - - uint32_t key = strtol(argv[2], NULL, 16); - do_encrypt(data, size, key); - - printf("#define DECRYPT_KEY 0x%08x\n", key); - for (int i = 0; i < size; i++) { - printf("0x%02x,", data[i]); - } - - return 0; -} + // 检查命令行参数是否正确(需要两个参数:文件名和密钥) + if (argc != 3) { + fprintf(stderr, "USAGE: encrypt \n"); + exit(-1); + } + + // 打开指定文件以二进制读取模式 + FILE *file = fopen(argv[1], "rb"); + if (!file) { + fprintf(stderr, "Can't open %s for reading\n", argv[1]); + exit(-1); + } + + // 获取文件大小 + long size = get_file_size(file); + // 分配内存以存储文件数据 + unsigned char *data = malloc(size); + if (!data) { + fprintf(stderr, "Can't allocate memory\n"); + exit(-1); + } + + // 从文件中读取数据到分配的内存中 + if (fread(data, size, 1, file) != 1) { + fprintf(stderr, "Can't read data from file\n"); + exit(-1); + } + + // 关闭文件 + fclose(file); + + // 将命令行传入的密钥从十六进制字符串转换为uint32_t整数 + uint32_t key = strtol(argv[2], NULL, 16); + // 使用指定的密钥对数据进行加密 + do_encrypt(data, size, key); + + // 输出解密密钥(宏定义形式) + printf("#define DECRYPT_KEY 0x%08x\n", key); + // 输出加密后的数据(十六进制格式) + for (int i = 0; i < size; i++) { + printf("0x%02x,", data[i]); + } + + // 释放分配的内存 + free(data); + + return 0; +} \ No newline at end of file diff --git a/src/Reptile/reptile.code-workspace b/src/Reptile/reptile.code-workspace new file mode 100644 index 0000000..d18652a --- /dev/null +++ b/src/Reptile/reptile.code-workspace @@ -0,0 +1,12 @@ +{ + "folders": [ + { + "path": "../.." + }, + { + "name": "reptile", + "path": "../../reptile" + } + ], + "settings": {} +} \ No newline at end of file