增加了文件传输和运行中修改远程IP端口的功能
This commit is contained in:
@@ -2,6 +2,10 @@
|
||||
## 使用方法
|
||||
需要使用openssl进行密钥生成,私钥保存为local.key放置于与主程序相同目录的位置。
|
||||
通信双方请提前交换公钥,程序不会传输公钥。
|
||||
程序使用UDP传输数据,并且使用点对点连接。如果在nat后需要使用内网穿透。
|
||||
## 待办
|
||||
传输文件
|
||||
程序使用UDP传输数据,并且使用点对点连接。如果在nat后需要使用内网穿透。
|
||||
支持传输文件,要注意的是不太支持特大的文件传输,因为RSA很慢。
|
||||
## 注意事项
|
||||
1.传输文件时接收方不能接收其他消息。
|
||||
2.不支持通过Base64传输文件。
|
||||
3.传输文件有可能损坏,建议两边手动通过哈希值确定文件状态。
|
||||
4.关键字:`!!!FILE!!!`作为传输文件的标识符不可以随便使用,如果必须传这个请不要放在文本头。
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
C++ version:
|
||||
C version:
|
||||
openssl 3.0 or later
|
||||
python version:
|
||||
Python 3.8 or later
|
||||
|
||||
+94
-45
@@ -1,18 +1,6 @@
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "RSA-Chat.h"
|
||||
#include "text.c"
|
||||
#include "file.c"
|
||||
|
||||
int listen_mode=1;int send_mode=1;
|
||||
|
||||
@@ -30,22 +18,49 @@ void* receive_f(void* _i){
|
||||
if(input->sockfd<0||!listen_mode){
|
||||
return NULL;
|
||||
}
|
||||
char buffer[BUFFER]={0};
|
||||
while(!stop_flag){
|
||||
ssize_t received = recvfrom(input->sockfd, buffer, sizeof(buffer), 0,NULL, NULL);
|
||||
char* buffer = malloc(BUFFER);
|
||||
char* filename = malloc(BUFFER);
|
||||
long file_size=0;
|
||||
while(1){
|
||||
ssize_t received = recvfrom(input->sockfd, buffer, BUFFER, 0,NULL, NULL);
|
||||
if(received<=0){
|
||||
perror("receive_f(): recvfrom failed.");
|
||||
}
|
||||
char* output = malloc(received);
|
||||
int size = text_decrypt(input->key,buffer,output);
|
||||
if(size < 0){
|
||||
return NULL;
|
||||
}
|
||||
output = realloc(output,size);
|
||||
printf("\033[32m%s\033[0m\n",output);
|
||||
free(output);
|
||||
char* header = malloc(11);
|
||||
memcpy(header,output,10);
|
||||
FILE* write;
|
||||
if(strcmp(header,"!!!FILE!!!")==0){
|
||||
strcpy(filename,output+10+sizeof(long));
|
||||
write = fopen(filename,"a");
|
||||
memcpy(&file_size,output+10,sizeof(long));
|
||||
}else if(file_size>0){
|
||||
fwrite(output,1,size,write);
|
||||
file_size-=size;
|
||||
if(file_size==0){
|
||||
printf("\033[32m已接收文件: %s\033[0m\n",filename);
|
||||
fclose(write);
|
||||
}
|
||||
if(file_size<0){
|
||||
perror("receive_f(): file receive failed.");
|
||||
file_size=0;
|
||||
fclose(write);
|
||||
}
|
||||
}else{
|
||||
output = realloc(output,size);
|
||||
printf("\033[32m%s\033[0m\n",output);
|
||||
free(output);
|
||||
}
|
||||
free(header);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
enum command_type{help=0,receive,send_c,quit,unknown};
|
||||
enum command_type{help=0,receive,send_c,quit,change,unknown};
|
||||
|
||||
enum command_type command(char* _i){
|
||||
if(strcmp(_i,"h")==0||strcmp(_i,"help")==0||strcmp(_i,"?")==0){
|
||||
@@ -56,6 +71,8 @@ enum command_type command(char* _i){
|
||||
return send_c;
|
||||
}else if(strcmp(_i,"q")==0||strcmp(_i,"quit")==0||strcmp(_i,"exit")==0){
|
||||
return quit;
|
||||
}else if(strcmp(_i,"c")==0||strcmp(_i,"change")==0){
|
||||
return change;
|
||||
}else{
|
||||
return unknown;
|
||||
}
|
||||
@@ -63,8 +80,8 @@ enum command_type command(char* _i){
|
||||
|
||||
int main(int argn,char** argc){
|
||||
if(argn==1){
|
||||
printf("Useage: %s <port> <peer public key file path>\n",argc[0]);
|
||||
printf("Useage: %s --offline <peer public key file path>\n",argc[0]);
|
||||
printf("用法: %s <本地端口> <远程公钥路径>\n",argc[0]);
|
||||
printf(" %s --offline <远程公钥路径>\n",argc[0]);
|
||||
return 0;
|
||||
}
|
||||
FILE* peer_key_file = fopen(argc[2],"r");
|
||||
@@ -72,7 +89,7 @@ int main(int argn,char** argc){
|
||||
EVP_PKEY* peer_key = EVP_PKEY_new();char* peer_key_c = malloc(BUFFER);
|
||||
int peer_bits=0;int local_bits=0;
|
||||
if(peer_key_file==NULL){
|
||||
printf("Peer public key read failed ,send mode will be disabled.\n");
|
||||
printf("远程公钥加载失败,发送模式将被禁用\n");
|
||||
send_mode=0;
|
||||
}else{
|
||||
size_t peer_key_file_size = fread(peer_key_c,1,BUFFER,peer_key_file);
|
||||
@@ -84,7 +101,7 @@ int main(int argn,char** argc){
|
||||
}
|
||||
FILE* local_key_file = fopen("local.key","r");
|
||||
if(local_key_file==NULL){
|
||||
printf("Local private key read failed ,listen/receive mode will be disabled.\n");
|
||||
printf("本地私钥加载失败,监听模式将被禁用\n");
|
||||
listen_mode=0;
|
||||
}else{
|
||||
size_t local_key_file_size = fread(local_key_c,1,BUFFER,local_key_file);
|
||||
@@ -99,18 +116,18 @@ int main(int argn,char** argc){
|
||||
if(strcmp(argc[1],"--offline")!=0){
|
||||
sockfd = socket(AF_INET6,SOCK_DGRAM,0);
|
||||
if(sockfd < 0){
|
||||
perror("Socket creation failed");
|
||||
perror("套接字创建失败");
|
||||
return -1;
|
||||
}
|
||||
int no = 0;
|
||||
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no))) {
|
||||
perror("Setsockopt IPV6_V6ONLY failed");
|
||||
perror("Setsockopt IPV6_V6ONLY失败");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
unsigned short int port = atoi(argc[1]);
|
||||
if(port>65535){
|
||||
perror("Wrong port number");
|
||||
perror("错误端口数字");
|
||||
return -1;
|
||||
}
|
||||
struct sockaddr_in6 serverAddr;
|
||||
@@ -118,7 +135,7 @@ int main(int argn,char** argc){
|
||||
serverAddr.sin6_port = htons(port);
|
||||
serverAddr.sin6_addr = in6addr_any;
|
||||
if (bind(sockfd, &serverAddr, sizeof(serverAddr))) {
|
||||
perror("bind failed");
|
||||
perror("bind失败");
|
||||
close(sockfd);
|
||||
return 1;
|
||||
}
|
||||
@@ -126,19 +143,18 @@ int main(int argn,char** argc){
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
printf("UDP socket opened on port %u\n",port);
|
||||
printf("UDP套接字已建立在端口%u\n",port);
|
||||
if(send_mode){
|
||||
char* buffer = malloc(BUFFER);
|
||||
printf("Please enter <remote IP> <PORT>:\n");
|
||||
printf("请输入 <远程IP> <端口>:\n");
|
||||
retry:
|
||||
memset(buffer,0,strlen(buffer));
|
||||
char peer_port[10]={0};
|
||||
scanf("%s%s",buffer,peer_port);
|
||||
if (getaddrinfo(buffer,peer_port, &hints, &result)) {
|
||||
perror("Wrong IP or PORT , please try again:\n$ ");
|
||||
perror("错误的地址或端口,请重试:\n$ ");
|
||||
goto retry;
|
||||
}
|
||||
memset(buffer,0,BUFFER);
|
||||
free(buffer);
|
||||
}
|
||||
}
|
||||
@@ -147,23 +163,24 @@ int main(int argn,char** argc){
|
||||
char* buffer = malloc(BUFFER);
|
||||
char* ioput = malloc(BUFFER);
|
||||
char input[BUFFER]={0};
|
||||
printf("初始化成功\n");
|
||||
while(1){
|
||||
memset(input,0,BUFFER);
|
||||
scanf("%s",input);
|
||||
switch(command(input)){
|
||||
case receive:
|
||||
if(!listen_mode){
|
||||
printf("Receive mode is disabled\n");
|
||||
printf("接收模式已被禁用\n");
|
||||
break;
|
||||
}
|
||||
printf("Please paste base64 code:\n");
|
||||
printf("请粘贴Base64字符串:\n");
|
||||
scanf("%s",ioput);
|
||||
base64_decode(ioput,buffer,NULL);
|
||||
memset(ioput,0,BUFFER);
|
||||
int size = text_decrypt(local_key,buffer,ioput);
|
||||
memset(buffer,0,BUFFER);
|
||||
if(size < 0){
|
||||
perror("Decrypt failed.");
|
||||
perror("解密失败");
|
||||
}else{
|
||||
printf("%s\n",ioput);
|
||||
}
|
||||
@@ -171,24 +188,35 @@ int main(int argn,char** argc){
|
||||
break;
|
||||
case send_c:
|
||||
if(!send_mode){
|
||||
printf("Send mode is disabled\n");
|
||||
printf("发送模式已被禁用\n");
|
||||
break;
|
||||
}
|
||||
printf("Please enter text:\n");
|
||||
printf("请输入要发送的文本或文件路径:\n");
|
||||
scanf("%s",ioput);
|
||||
if(strlen(ioput)>(peer_bits/8-48)){
|
||||
FILE* send_file = fopen(ioput,"r");
|
||||
if(strlen(ioput)>(peer_bits/8-80)){
|
||||
perror("Text too long for key of peer.\n");
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
size = text_encrypt(peer_key,ioput,buffer);
|
||||
if(send_file!=NULL){
|
||||
int send_stat = send_file_f(ioput,sockfd,peer_key,result);
|
||||
fclose(send_file);
|
||||
if(send_stat!=0){
|
||||
perror("文件发送失败");
|
||||
}else{
|
||||
printf("\033[32m文件发送成功: %s\033[0m\n",ioput);
|
||||
}
|
||||
break;
|
||||
}
|
||||
size = text_encrypt(peer_key,ioput,buffer,strlen(ioput));
|
||||
memset(ioput,0,BUFFER);
|
||||
if(size < 0){
|
||||
perror("Encrypt failed.");
|
||||
perror("加密失败");
|
||||
}else{
|
||||
if(sockfd>0){
|
||||
ssize_t sent = sendto(sockfd, buffer, size, 0,result->ai_addr, result->ai_addrlen);
|
||||
if (sent < 0){
|
||||
perror("sendto failed");
|
||||
perror("sendto失败");
|
||||
}
|
||||
}
|
||||
base64_encode(buffer,size,ioput);
|
||||
@@ -205,11 +233,32 @@ int main(int argn,char** argc){
|
||||
pthread_cancel(receive_t);
|
||||
pthread_join(receive_t,NULL);
|
||||
return 0;
|
||||
case help:
|
||||
printf("Useage:\n send/s | Send text to remote or stdout with base64.\n recv/r | Decrypt base64 and output into stdout.\n help/h/? | Show this page.\n");
|
||||
case change:
|
||||
struct addrinfo hints={0};
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
printf("请输入 <远程IP> <端口>:\n");
|
||||
memset(buffer,0,strlen(buffer));
|
||||
char peer_port[10]={0};
|
||||
while(1){
|
||||
scanf("%s%s",buffer,peer_port);
|
||||
if (getaddrinfo(buffer,peer_port, &hints, &result)) {
|
||||
perror("错误的地址或端口,请重试:\n ");
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
memset(buffer,0,BUFFER);
|
||||
break;
|
||||
case help:
|
||||
default:
|
||||
printf("Useage:\n send/s | Send text to remote or stdout with base64.\n recv/r | Decrypt base64 and output into stdout.\n help/h/? | Show this page.\n");
|
||||
printf("命令:\n");
|
||||
printf(" send/s [文件路径/密文] | 发送密文或文件至远程地址或将密文编码为Base64输出至stdout\n");
|
||||
printf(" recv/r [Base64密文] | 解密Base64编码的密文并输出至stdout\n");
|
||||
printf(" change/c [IP] [端口] | 修改远程IP\n");
|
||||
printf(" help/h/? | 展示当前页面\n");
|
||||
printf(" q/quit/exit | 退出程序\n");
|
||||
}
|
||||
}
|
||||
stop_flag = 1;
|
||||
|
||||
+14
-1
@@ -1,2 +1,15 @@
|
||||
#define PADDING_METHOD RSA_PKCS1_OAEP_PADDING
|
||||
#define BUFFER 4096
|
||||
#define BUFFER 4096
|
||||
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <pthread.h>
|
||||
@@ -0,0 +1,82 @@
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/err.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "RSA-Chat.h"
|
||||
#include "text.c"
|
||||
|
||||
#ifndef _FILE_C
|
||||
#define _FILE_C
|
||||
|
||||
const char* get_filename(const char *path) {
|
||||
if (path == NULL || *path == '\0') {
|
||||
return ".";
|
||||
}
|
||||
const char *end = path + strlen(path) - 1;
|
||||
while (end > path && *end == '/') {
|
||||
end--;
|
||||
}
|
||||
if (end == path && *end == '/') {
|
||||
return "/";
|
||||
}
|
||||
const char *slash = end;
|
||||
while (slash > path && *slash != '/') {
|
||||
slash--;
|
||||
}
|
||||
if (*slash == '/') {
|
||||
return slash + 1;
|
||||
} else {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
int send_file_f(const char* _path,int _sockfd,EVP_PKEY* _pubkey,struct addrinfo* _peer){
|
||||
FILE* _f = fopen(_path,"r");
|
||||
if(_f==NULL){
|
||||
return -1;
|
||||
}
|
||||
long current_pos = ftell(_f);
|
||||
fseek(_f, 0, SEEK_END);
|
||||
long file_size = ftell(_f);
|
||||
fseek(_f, current_pos, SEEK_SET);
|
||||
if(_sockfd<0){
|
||||
return -1;
|
||||
}
|
||||
char* block = malloc(EVP_PKEY_bits(_pubkey)/8-80);
|
||||
memcpy(block,"!!!FILE!!!",10);
|
||||
memcpy(block+10,&file_size,sizeof(long));
|
||||
const char* filename;
|
||||
{
|
||||
filename = get_filename(_path);
|
||||
memcpy(block+sizeof(long)+10,filename,strlen(filename));
|
||||
}
|
||||
char* header4send = malloc(BUFFER);
|
||||
size_t enc_header = text_encrypt(_pubkey,block,header4send,10+sizeof(long)+strlen(filename));
|
||||
header4send = realloc(header4send,enc_header);
|
||||
size_t send_bytes = sendto(_sockfd,header4send,enc_header,0,_peer->ai_addr,_peer->ai_addrlen);
|
||||
free(header4send);
|
||||
if (send_bytes < 0){
|
||||
perror("sendto failed");
|
||||
return -1;
|
||||
}
|
||||
for(int i=0;i<file_size+1;i++){
|
||||
if((i%(EVP_PKEY_bits(_pubkey)/8-80)==0&&i!=0)||i==file_size){
|
||||
char* ready4send = malloc(BUFFER);
|
||||
size_t enc = text_encrypt(_pubkey,block,ready4send,file_size-i?EVP_PKEY_bits(_pubkey)/8-80:file_size%(EVP_PKEY_bits(_pubkey)/8-80));
|
||||
ready4send = realloc(ready4send,enc);
|
||||
size_t send_bytes = sendto(_sockfd,ready4send,enc,0,_peer->ai_addr,_peer->ai_addrlen);
|
||||
free(ready4send);
|
||||
if (send_bytes < 0){
|
||||
perror("sendto failed");
|
||||
return -1;
|
||||
}
|
||||
memset(block,0,EVP_PKEY_bits(_pubkey)/8-80);
|
||||
}
|
||||
block[i%(EVP_PKEY_bits(_pubkey)/8-80)] = getc(_f);
|
||||
}
|
||||
free(block);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
+9
-5
@@ -1,4 +1,3 @@
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/err.h>
|
||||
@@ -6,7 +5,10 @@
|
||||
|
||||
#include "RSA-Chat.h"
|
||||
|
||||
int text_encrypt(EVP_PKEY* key,char *_i,char *_o){
|
||||
#ifndef _TEXT_C
|
||||
#define _TEXT_C
|
||||
|
||||
int text_encrypt(EVP_PKEY* key,char *_i,char *_o,int _len){
|
||||
EVP_PKEY_CTX* key_ctx = EVP_PKEY_CTX_new(key, NULL);
|
||||
if(key_ctx==NULL){
|
||||
ERR_print_errors_fp(stderr);
|
||||
@@ -18,13 +20,13 @@ int text_encrypt(EVP_PKEY* key,char *_i,char *_o){
|
||||
EVP_PKEY_CTX_set_rsa_oaep_md(key_ctx, EVP_sha256());
|
||||
EVP_PKEY_CTX_set_rsa_mgf1_md(key_ctx, EVP_sha256());
|
||||
size_t enc;
|
||||
int stat = EVP_PKEY_encrypt(key_ctx,NULL,&enc,(const unsigned char *)_i,strlen(_i));
|
||||
int stat = EVP_PKEY_encrypt(key_ctx,NULL,&enc,(const unsigned char *)_i,_len);
|
||||
if(!stat){
|
||||
ERR_print_errors_fp(stderr);
|
||||
ERR_clear_error();
|
||||
return -1;
|
||||
}
|
||||
stat = EVP_PKEY_encrypt(key_ctx,(unsigned char *)_o,&enc,(const unsigned char *)_i,strlen(_i));
|
||||
stat = EVP_PKEY_encrypt(key_ctx,(unsigned char *)_o,&enc,(const unsigned char *)_i,_len);
|
||||
if(!stat){
|
||||
ERR_print_errors_fp(stderr);
|
||||
ERR_clear_error();
|
||||
@@ -150,4 +152,6 @@ void base64_decode(const char *input, unsigned char *output, int *output_len) {
|
||||
if(output_len!=NULL){
|
||||
*output_len = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user