From 1cee5d570f9bc31c6218b0c0d4cd5c8f905a7c58 Mon Sep 17 00:00:00 2001 From: tomyee <519395917@qq.com> Date: Wed, 18 Dec 2024 13:05:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=91=BD=E4=BB=A4=E5=88=A4=E6=96=AD=E8=BF=9B?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + files/20241217102536/123.txt => 123 | 0 README.md | 16 +- client | Bin 34904 -> 34840 bytes client.c | 43 ++-- files/20241217102043/test.enc | 1 - files/20241217102043/test.gz | Bin 71 -> 0 bytes files/20241217102043/test.txt | 2 - files/20241217103021/321.enc | 1 - files/20241217103021/321.gz | Bin 37 -> 0 bytes files/20241217103021/321.txt | 1 - files/20241217103853/test.txt | 2 - files/20241217111950/123.txt | 2 - files/20241218001945/DOWNLOAD123.txt.txt | 0 .../123.txt => 20241218125930/1234.txt} | 0 .../123.txt => 20241218130317/123455.txt} | 0 gcc.sh | 2 + newfile/1234.txt | 1 + newfile/123455.txt | 1 + newfile/{init.sh => 321.txt} | 0 server | Bin 36408 -> 36472 bytes server.c | 220 ++++++++++-------- 22 files changed, 170 insertions(+), 125 deletions(-) rename files/20241217102536/123.txt => 123 (100%) delete mode 100644 files/20241217102043/test.enc delete mode 100644 files/20241217102043/test.gz delete mode 100644 files/20241217102043/test.txt delete mode 100644 files/20241217103021/321.enc delete mode 100644 files/20241217103021/321.gz delete mode 100644 files/20241217103021/321.txt delete mode 100644 files/20241217103853/test.txt delete mode 100644 files/20241217111950/123.txt delete mode 100644 files/20241218001945/DOWNLOAD123.txt.txt rename files/{20241217104148/123.txt => 20241218125930/1234.txt} (100%) rename files/{20241218000302/123.txt => 20241218130317/123455.txt} (100%) create mode 100755 gcc.sh create mode 100644 newfile/1234.txt create mode 100644 newfile/123455.txt rename newfile/{init.sh => 321.txt} (100%) diff --git a/.gitignore b/.gitignore index 78966f9..7cd9f3c 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,6 @@ /out/* *.plist + +./files/* +./newfile/* \ No newline at end of file diff --git a/files/20241217102536/123.txt b/123 similarity index 100% rename from files/20241217102536/123.txt rename to 123 diff --git a/README.md b/README.md index 79898e4..b029ec3 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,21 @@ gcc -o server server.c -lpthread -L/usr/local/lib -I/usr/local/include -lgmssl - gcc -o client client.c ~~~ + + + + 启动测试: 1、启动服务端:./server 2、启动客户端:./client -3、客户端根据提示输入命令及内容 \ No newline at end of file +3、客户端根据提示输入命令及内容 + +启动报错处理 +tong ~/Tomyee/money/linux/code (master●●●)$ ./server +dyld[90941]: Library not loaded: @rpath/libgmssl.3.dylib + Referenced from: /Users/tong/Tomyee/money/linux/code/server + Reason: no LC_RPATH's found +[1] 90941 abort ./server + + +临时办法:export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH \ No newline at end of file diff --git a/client b/client index 9a3c20d2d8825e8adc2b4c1f89f45bddec53c84e..149278c124daed6fa561e00666972b53d53be4e8 100755 GIT binary patch delta 2782 zcmZ`*dr(x@89(K6qV|sUexxF;u5CW@?Bbm}q@eOzb3) z%Ui`Lrw0wDw%81|gmKgkPQq3&p_0Zp&NNWlAya317hSz5W2E5^ZXVL#xw{CnnVy;7 zJ@hH?oJ2q6hZJTQiG=~&5NZ1A9mnc~{|G9p6fJ_8y)70BCgu|z848v;36bOArqmHqfmsAhfK*S29dmXQA?sPB z!78M%H4&D`^^JtM=yOY1g&{p|v8iPAe_Mr?f~uq~2zivgu!mJYpbnd5%ch!P-V%$fo{Mxjc1NZp@~3ypJ0QRih+o zv?o$ElW6Gz(RBZz9BPHVR>_pK!AQA06M|AoG_{lmC6j7Wr@72PTnL!Jb>kq(0Etm} z>>jsm73XYz*zC4M>UR)r+#vLb@K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+%c+nxZ%;|wlW!9Ph|4%P59|s%Wt&>f6=%09rFhnJk&mIhT=hjuq*{l z!N)pWV1l1L#`n4S3+>gF&0rxG8e?Fe+{N!n<9m6$-JXL3b(htjxU<7fKjU>BcITIA zIxUUA!rNelV|*7vGL5(JBOUh0v{Y*2+097fA4ucprSak&PW=HMBbdfNoW|p}oMRu3p#j^!m-@ zk5$E%#T(2&wR|+Ese0tXf+-(0uaPbde$yJ7zI4gkp&_34ilPkfY5Vk(;-f>`uTKTX>9b xXmS=`;QW 0) { + compressed_path[bytes_received] = '\0'; // 确保字符串以空字符结尾 + printf("服务器发送的加密压缩包路径是: %s\n", compressed_path); + } else { + perror("读取服务器发送的路径失败"); + } } void download_file(int client_socket) { char output_file_name[BUFFER_SIZE]; - char file_name[BUFFER_SIZE]; - - printf("输入要下载的文件名(不带路径): "); - scanf("%s", file_name); + char file_path[BUFFER_SIZE]; + printf("输入要下载的文件(加密后的文件压缩包)完整路径: "); + scanf("%s", file_path); + // 发送下载请求到服务器 - send(client_socket, "DOWNLOAD", strlen("DOWNLOAD"), 0); - send(client_socket, file_name, strlen(file_name), 0); + send(client_socket, "download", strlen("download"), 0); + send(client_socket, file_path, strlen(file_path), 0); // 等待服务端准备完成的信号 char ack[BUFFER_SIZE] = {0}; int ack_read = read(client_socket, ack, BUFFER_SIZE); @@ -119,8 +132,8 @@ void download_file(int client_socket) return; } // 创建输出文件 - sprintf(output_file_name, "%s_output.txt", file_name); - FILE *output = fopen(output_file_name, "wb"); + + FILE *output = fopen("./output.txt", "wb"); if (!output) { perror("文件打开失败"); @@ -134,7 +147,7 @@ void download_file(int client_socket) fwrite(buffer, 1, bytes_read, output); } fclose(output); - printf("文件已下载,保存在 %s 中。\n", output_file_name); + printf("文件已下载,保存在当前目录下的output.txt中。\n"); } int main() @@ -171,18 +184,18 @@ int main() char command[10]; while (1) { - printf("请输入命令 (UPLOAD/DOWNLOAD/EXIT): "); + printf("请输入命令 (upload/download/exit): "); scanf("%s", command); - if (strcmp(command, "UPLOAD") == 0) + if (strcmp(command, "upload") == 0) { upload_file(client_socket); } - else if (strcmp(command, "DOWNLOAD") == 0) + else if (strcmp(command, "download") == 0) { download_file(client_socket); } - else if (strcmp(command, "EXIT") == 0) + else if (strcmp(command, "exit") == 0) { break; } diff --git a/files/20241217102043/test.enc b/files/20241217102043/test.enc deleted file mode 100644 index 97cf676..0000000 --- a/files/20241217102043/test.enc +++ /dev/null @@ -1 +0,0 @@ -sQ÷W%K)$o&TIwɽ5m=3o𯚑g \ No newline at end of file diff --git a/files/20241217102043/test.gz b/files/20241217102043/test.gz deleted file mode 100644 index af6f2994b31344c07a1f7f9df2b73c8eb8b4723b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 71 zcmb2|=3oE=VMYUn^Z$!4pAEcpc)Mh{q-yiR``({5RsO!+lCKux+4}Qr`N_Se+jDKN Z{rzE_f8+lB59?=5OlQ2cKi>eT9{?N~Aj|*& diff --git a/files/20241217102043/test.txt b/files/20241217102043/test.txt deleted file mode 100644 index 7f5281b..0000000 --- a/files/20241217102043/test.txt +++ /dev/null @@ -1,2 +0,0 @@ -123 -EOFDOWNLOADtest \ No newline at end of file diff --git a/files/20241217103021/321.enc b/files/20241217103021/321.enc deleted file mode 100644 index a8e1975..0000000 --- a/files/20241217103021/321.enc +++ /dev/null @@ -1 +0,0 @@ -;;Xg:L@A \ No newline at end of file diff --git a/files/20241217103021/321.gz b/files/20241217103021/321.gz deleted file mode 100644 index 8efd164a144f81573e3670443600054d5f95dab5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 37 ocmb2|=3oE=;oCn?M4hS%JaVmM^~%?^GV`pN7;4nA-w6O^0rZ^>9RL6T diff --git a/files/20241217103021/321.txt b/files/20241217103021/321.txt deleted file mode 100644 index 3ae0b93..0000000 --- a/files/20241217103021/321.txt +++ /dev/null @@ -1 +0,0 @@ -321 diff --git a/files/20241217103853/test.txt b/files/20241217103853/test.txt deleted file mode 100644 index ec22773..0000000 --- a/files/20241217103853/test.txt +++ /dev/null @@ -1,2 +0,0 @@ -123 -EOF \ No newline at end of file diff --git a/files/20241217111950/123.txt b/files/20241217111950/123.txt deleted file mode 100644 index c36f8ce..0000000 --- a/files/20241217111950/123.txt +++ /dev/null @@ -1,2 +0,0 @@ -123 -EOFDOWNLOAD123_decrypted.txt \ No newline at end of file diff --git a/files/20241218001945/DOWNLOAD123.txt.txt b/files/20241218001945/DOWNLOAD123.txt.txt deleted file mode 100644 index e69de29..0000000 diff --git a/files/20241217104148/123.txt b/files/20241218125930/1234.txt similarity index 100% rename from files/20241217104148/123.txt rename to files/20241218125930/1234.txt diff --git a/files/20241218000302/123.txt b/files/20241218130317/123455.txt similarity index 100% rename from files/20241218000302/123.txt rename to files/20241218130317/123455.txt diff --git a/gcc.sh b/gcc.sh new file mode 100755 index 0000000..831517a --- /dev/null +++ b/gcc.sh @@ -0,0 +1,2 @@ +gcc -o server server.c -lpthread -L/usr/local/lib -I/usr/local/include -lgmssl -lz +gcc -o client client.c \ No newline at end of file diff --git a/newfile/1234.txt b/newfile/1234.txt new file mode 100644 index 0000000..939b0cb --- /dev/null +++ b/newfile/1234.txt @@ -0,0 +1 @@ +1233 diff --git a/newfile/123455.txt b/newfile/123455.txt new file mode 100644 index 0000000..81c545e --- /dev/null +++ b/newfile/123455.txt @@ -0,0 +1 @@ +1234 diff --git a/newfile/init.sh b/newfile/321.txt similarity index 100% rename from newfile/init.sh rename to newfile/321.txt diff --git a/server b/server index 6a2f32cf8fa43e67c71de2b80f0055ff6c4bbbc5..2d1ae413f53bee2a6ede4783511713d418bae067 100755 GIT binary patch delta 4999 zcmZ`-3s_WD9zXZaOim<=C<1B-BDasq9c8udZ5MztMYD?B6! ziA)jyB0S9UP06D}VT#U7huv{X!2G;!JtG8{XT)@uRUj)oXLpSD5R!%r2Jy1Algb3z1w}6-N|iu@E{$00 z^1$dtlXXvw;I}15AX6`z2(<~Qz@`i;R}jS%;W*_snf8dpF%N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p5zhCefU=f0gv$o~O#kDWRI diff --git a/server.c b/server.c index b97640a..68e4fb6 100644 --- a/server.c +++ b/server.c @@ -20,7 +20,9 @@ #define PORT 8001 #define MAX_CLIENTS 5 #define BUFFER_SIZE 1024 - +// 密钥和偏移量 +uint8_t key[SM4_KEY_SIZE]; +uint8_t iv[SM4_BLOCK_SIZE]; // 将信号量定义为全局变量 dispatch_semaphore_t semaphore; int log_pipe[2]; // 日志记录的管道 @@ -41,6 +43,8 @@ void create_timestamp_dir(char *dir_name) t->tm_hour, t->tm_min, t->tm_sec); mkdir("./files", 0777); mkdir(dir_name, 0777); + // 创建解密结果文件目录 + mkdir("./files/decrypt", 0777); } // 记录日志 @@ -197,131 +201,118 @@ void *client_handler(void *arg) // 初始化缓冲区为 0 memset(buffer, 0, BUFFER_SIZE); - // 接收文件名 - int bytes = read(client_socket, buffer, BUFFER_SIZE); + // 接收第一个参数判断参数客户端需要上传还是下载 + int bytes = read(client_socket, buffer, BUFFER_SIZE - 1);// 预留一个字节用于NULL终止符 if (bytes <= 0) { - perror("接收文件名失败"); + perror("接收客户端命令失败"); return NULL; } buffer[bytes] = '\0'; // 确保以 NULL 结尾 - char file_name[256]; - snprintf(file_name, sizeof(file_name), "%s", buffer); - printf("接收文件名:%s\n", file_name); - // 发送 ACK 确认 - send(client_socket, "ACK", strlen("ACK"), 0); + char upload_or_download[256] ; + snprintf(upload_or_download, sizeof(upload_or_download), "%s", buffer); + printf("客户端发起请求:%s,等待后续指令....\n", upload_or_download); char origin_file_path[256], dir_name[128], file_path[256], compressed_file[256], decrypted_file[256]; - // 先生成./files目录,再生成时间戳目录,调用 create_timestamp_dir 函数,生成一个基于时间戳的目录名称,并将其存储在 dir_name 中 - create_timestamp_dir(dir_name); - // 根据传入的文件名以及时间戳拼接生成源文件路径、加密后的文件、压缩的文件、解密的文件路径 - sprintf(origin_file_path, "%s/%s.txt", dir_name, buffer); - sprintf(file_path, "%s/%s.enc", dir_name, buffer); - sprintf(compressed_file, "%s/%s.gz", dir_name, buffer); - // sprintf(decrypted_file, sizeof(decrypted_file), "%s/%s_decrypted.txt", dir_name, buffer); - snprintf(decrypted_file, sizeof(decrypted_file), "%s/%s_decrypted.txt", dir_name, buffer); - printf("源文件路径%s\n加密后的文件%s\n压缩的文件%s\n解密的文件路径%s\n", origin_file_path, file_path, compressed_file, decrypted_file); - FILE *file = fopen(origin_file_path, "wb"); - if (!file) - { - perror("文件打开失败"); - return NULL; - } - // 按块读取源文件内容并保存到origin_file_path - while (1) + if (strcmp(upload_or_download, "upload") == 0) { + // 接收文件名(不含后缀) int bytes = read(client_socket, buffer, BUFFER_SIZE); - if (bytes < 0) + if (bytes <= 0) { - perror("读取数据失败"); - fclose(file); + perror("接收文件名失败"); return NULL; } - // 检测是否接收到结束标志 - if (strncmp(buffer, "EOF", 3) == 0) - break; - // 将数据写入文件 - if (fwrite(buffer, 1, bytes, file) != bytes) + buffer[bytes] = '\0'; // 确保以 NULL 结尾 + char file_name[256]; + snprintf(file_name, sizeof(file_name), "%s", buffer); + printf("接收文件名:%s\n", file_name); + // 发送 ACK 确认 + send(client_socket, "ACK", strlen("ACK"), 0); + + // 先生成./files目录,再生成时间戳目录,调用 create_timestamp_dir 函数,生成一个基于时间戳的目录名称,并将其存储在 dir_name 中 + create_timestamp_dir(dir_name); + + // 根据传入的文件名以及时间戳拼接生成源文件路径 + sprintf(origin_file_path, "%s/%s.txt", dir_name, buffer); + // 加密后的文件路径 + sprintf(file_path, "%s/%s.enc", dir_name, buffer); + // 加密后压缩包的文件路径 + sprintf(compressed_file, "%s/%s.gz", dir_name, buffer); + // 解密的文件路径 + snprintf(decrypted_file, sizeof(decrypted_file), "./files/decrypt/%s_decrypted.txt", buffer); + printf("源文件路径:%s\n加密后的文件:%s\n压缩的文件:%s\n解密的文件路径:%s\n", origin_file_path, file_path, compressed_file, decrypted_file); + + FILE *file = fopen(origin_file_path, "wb"); + if (!file) { - perror("写入文件失败"); - fclose(file); + perror("文件打开失败"); return NULL; } + // 按块读取源文件内容并保存到origin_file_path + while (1) + { + int bytes = read(client_socket, buffer, BUFFER_SIZE -1); + if (bytes < 0) + { + perror("读取数据失败"); + fclose(file); + return NULL; + } + // 检测是否接收到结束标志 + if (strncmp(buffer, "EOF", 3) == 0) + break; + // 将数据写入文件 + if (fwrite(buffer, 1, bytes, file) != bytes) + { + perror("写入文件失败"); + fclose(file); + return NULL; + } + } + fclose(file); + + log_operation("文件已接收。\n"); + + // 加密文件 + printf("正在加密文件...\n"); + sm4_cbc_padding_encrypt_file(origin_file_path, file_path, key, iv); + printf("文件加密完成。\n"); + char log_message_file_path[BUFFER_SIZE]; + sprintf(log_message_file_path, "文件已加密。加密文件保存路径为:%s\n", file_path); + log_operation(log_message_file_path); + // 压缩文件 + printf("正在压缩被加密的文件...\n"); + compress_file(file_path, compressed_file); + printf("文件压缩完成。\n"); + + // 删除加密文件 + // remove(file_path); + char log_message_compressed_file[BUFFER_SIZE]; + sprintf(log_message_compressed_file, "文件已加密。加密文件保存路径为:%s\n", compressed_file); + + // 发送压缩的加密文件路径给客户端 + send(client_socket, compressed_file, strlen(compressed_file), 0); + log_operation(log_message_compressed_file); } - fclose(file); - - log_operation("文件已接收。\n"); - - // 随机生成加密密钥和 IV - uint8_t key[SM4_KEY_SIZE]; - if (rand_bytes(key, SM4_KEY_SIZE) != 1) - { - fprintf(stderr, "随机密钥生成失败。\n"); - return NULL; - } - uint8_t iv[SM4_BLOCK_SIZE]; - if (rand_bytes(iv, SM4_BLOCK_SIZE) != 1) - { - fprintf(stderr, "随机IV生成失败。\n"); - return NULL; - } - - // 打印密钥和 IV - printf("随机生成的密钥: "); - for (size_t i = 0; i < SM4_KEY_SIZE; i++) - { - printf("%02X ", key[i]); - } - printf("\n"); - - printf("随机生成的IV: "); - for (size_t i = 0; i < SM4_BLOCK_SIZE; i++) - { - printf("%02X ", iv[i]); - } - printf("\n"); - - // 加密文件 - printf("正在加密文件...\n"); - sm4_cbc_padding_encrypt_file(origin_file_path, file_path, key, iv); - printf("文件加密完成。\n"); - char log_message_file_path[BUFFER_SIZE]; - sprintf(log_message_file_path, "文件已加密。加密文件保存路径为:%s\n", file_path); - log_operation(log_message_file_path); - // 压缩文件 - printf("正在压缩被加密的文件...\n"); - compress_file(file_path, compressed_file); - printf("文件压缩完成。\n"); - - // 删除加密文件 - // remove(file_path); - char log_message_compressed_file[BUFFER_SIZE]; - sprintf(log_message_compressed_file, "文件已加密。加密文件保存路径为:%s\n", compressed_file); - log_operation(log_message_compressed_file); - - // 等待客户端请求 - memset(buffer, 0, BUFFER_SIZE); - read(client_socket, buffer, BUFFER_SIZE); - - if (strcmp(buffer, "DOWNLOAD") == 0) + else if (strcmp(upload_or_download, "download") == 0) { printf("接收到下载请求...\n"); - // 读取文件名 memset(buffer, 0, BUFFER_SIZE); + // 读取文件路径 read(client_socket, buffer, BUFFER_SIZE); - char requested_file[256]; - snprintf(requested_file, sizeof(requested_file), "%s", buffer); - printf("客户端请求的文件名: %s\n", requested_file); - printf("接收到下载请求,正在解压缩文件...\n"); - decompress_file(compressed_file, decrypted_file); + + sprintf(compressed_file, "%s", buffer); + + // 解压缩、解密并发送给客户端 + decompress_file(compressed_file, compressed_file); printf("文件解压缩完成。\n"); printf("正在解密文件...\n"); sm4_cbc_padding_decrypt_file(decrypted_file, decrypted_file, key, iv); printf("文件解密完成。\n"); - log_operation("文件已解压并解密,准备发送。\n"); // 向客户端发送 "READY" 信号 send(client_socket, "READY", strlen("READY"), 0); @@ -339,7 +330,10 @@ void *client_handler(void *arg) // 删除解密后的文件 // remove(decrypted_file); } - + else + { + perror("读取客户端命令失败"); + } // 关闭连接 close(client_socket); dispatch_semaphore_signal(semaphore); @@ -390,6 +384,32 @@ int main() return -1; } + // 随机生成加密密钥和 IV + if (rand_bytes(key, SM4_KEY_SIZE) != 1) + { + fprintf(stderr, "随机密钥生成失败。\n"); + return -1; + } + if (rand_bytes(iv, SM4_BLOCK_SIZE) != 1) + { + fprintf(stderr, "随机IV生成失败。\n"); + return -1; + } + // 打印密钥和 IV + printf("随机生成的密钥: "); + for (size_t i = 0; i < SM4_KEY_SIZE; i++) + { + printf("%02X ", key[i]); + } + printf("\n"); + + printf("随机生成的IV: "); + for (size_t i = 0; i < SM4_BLOCK_SIZE; i++) + { + printf("%02X ", iv[i]); + } + printf("\n"); + // 示例:客户端处理逻辑 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // 等待信号量 // printf("客户端进入,当前资源被占用\n");