代码注释

main^2
shenzexi 8 months ago
parent 10e0d3a49b
commit 658f630317

@ -4,49 +4,71 @@
#include "encrypt.h" #include "encrypt.h"
// 函数声明:获取文件大小
// 参数:指向文件的指针
// 返回值:文件的大小(字节数)
static long get_file_size(FILE *file) static long get_file_size(FILE *file)
{ {
long size; long size;
fseek(file, 0, SEEK_END); // 将文件指针移动到文件末尾
size = ftell(file); fseek(file, 0, SEEK_END);
rewind(file); // 获取文件指针当前位置,即文件大小
return size; size = ftell(file);
// 将文件指针重新移动到文件开头
rewind(file);
return size;
} }
// 主函数:程序入口
// 参数:命令行参数个数,命令行参数数组
// 返回值:程序退出状态码
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc != 3) { // 检查命令行参数是否正确(需要两个参数:文件名和密钥)
fprintf(stderr, "USAGE: encrypt <file> <pass:hex(uint32)>\n"); if (argc != 3) {
exit(-1); fprintf(stderr, "USAGE: encrypt <file> <pass:hex(uint32)>\n");
} exit(-1);
}
FILE *file = fopen(argv[1], "rb");
if (!file) { // 打开指定文件以二进制读取模式
fprintf(stderr, "Can't open %s for reading\n", argv[1]); FILE *file = fopen(argv[1], "rb");
exit(-1); 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"); long size = get_file_size(file);
exit(-1); // 分配内存以存储文件数据
} unsigned char *data = malloc(size);
if (!data) {
if (fread(data, size, 1, file) != 1) { fprintf(stderr, "Can't allocate memory\n");
fprintf(stderr, "Can't read data from file\n"); exit(-1);
exit(-1); }
}
// 从文件中读取数据到分配的内存中
fclose(file); if (fread(data, size, 1, file) != 1) {
fprintf(stderr, "Can't read data from file\n");
uint32_t key = strtol(argv[2], NULL, 16); exit(-1);
do_encrypt(data, size, key); }
printf("#define DECRYPT_KEY 0x%08x\n", key); // 关闭文件
for (int i = 0; i < size; i++) { fclose(file);
printf("0x%02x,", data[i]);
} // 将命令行传入的密钥从十六进制字符串转换为uint32_t整数
uint32_t key = strtol(argv[2], NULL, 16);
return 0; // 使用指定的密钥对数据进行加密
} 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;
}

@ -0,0 +1,12 @@
{
"folders": [
{
"path": "../.."
},
{
"name": "reptile",
"path": "../../reptile"
}
],
"settings": {}
}
Loading…
Cancel
Save