Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8efbeaef0a | ||
|
|
6d92a7c9bb |
@@ -1,2 +1,2 @@
|
|||||||
all:
|
all:
|
||||||
gcc -o RSA-Chat src/c/RSA-Chat.c -lcrypto
|
gcc -o RSA-Chat src/c/RSA-Chat.c -lcrypto -lreadline
|
||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
C version:
|
C version:
|
||||||
openssl 3.0 or later
|
openssl 3.0
|
||||||
|
readline 8.2-1.3
|
||||||
python version:
|
python version:
|
||||||
Python 3.8 or later
|
Python 3.8 or later
|
||||||
cryptography >= 38.0.0
|
cryptography >= 38.0.0
|
||||||
|
|||||||
+28
-14
@@ -2,6 +2,9 @@
|
|||||||
#include "text.c"
|
#include "text.c"
|
||||||
#include "file.c"
|
#include "file.c"
|
||||||
|
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
|
|
||||||
int listen_mode=1;int send_mode=1;
|
int listen_mode=1;int send_mode=1;
|
||||||
|
|
||||||
int stop_flag=0;
|
int stop_flag=0;
|
||||||
@@ -22,10 +25,14 @@ void* receive_f(void* _i){
|
|||||||
char* filename = malloc(BUFFER);
|
char* filename = malloc(BUFFER);
|
||||||
long file_size=0;
|
long file_size=0;
|
||||||
while(1){
|
while(1){
|
||||||
|
memset(buffer,0,BUFFER);
|
||||||
ssize_t received = recvfrom(input->sockfd, buffer, BUFFER, 0,NULL, NULL);
|
ssize_t received = recvfrom(input->sockfd, buffer, BUFFER, 0,NULL, NULL);
|
||||||
if(received<=0){
|
if(received<=0){
|
||||||
perror("receive_f(): recvfrom failed.");
|
perror("receive_f(): recvfrom failed.");
|
||||||
}
|
}
|
||||||
|
rl_save_prompt();
|
||||||
|
printf("\033[2K\r");
|
||||||
|
fflush(stdout);
|
||||||
char* output = malloc(received);
|
char* output = malloc(received);
|
||||||
int size = text_decrypt(input->key,buffer,output);
|
int size = text_decrypt(input->key,buffer,output);
|
||||||
if(size < 0){
|
if(size < 0){
|
||||||
@@ -55,6 +62,8 @@ void* receive_f(void* _i){
|
|||||||
printf("\033[32m%s\033[0m\n",output);
|
printf("\033[32m%s\033[0m\n",output);
|
||||||
free(output);
|
free(output);
|
||||||
}
|
}
|
||||||
|
rl_restore_prompt();
|
||||||
|
rl_forced_update_display();
|
||||||
free(header);
|
free(header);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -161,22 +170,29 @@ int main(int argn,char** argc){
|
|||||||
pthread_t receive_t;struct receive_input receive_i = {sockfd, local_key, result};
|
pthread_t receive_t;struct receive_input receive_i = {sockfd, local_key, result};
|
||||||
pthread_create(&receive_t,NULL,receive_f,&receive_i);
|
pthread_create(&receive_t,NULL,receive_f,&receive_i);
|
||||||
char* buffer = malloc(BUFFER);
|
char* buffer = malloc(BUFFER);
|
||||||
|
memset(buffer,0,BUFFER);
|
||||||
char* ioput = malloc(BUFFER);
|
char* ioput = malloc(BUFFER);
|
||||||
char input[BUFFER]={0};
|
memset(ioput,0,BUFFER);
|
||||||
|
char* cmd = malloc(BUFFER);
|
||||||
|
memset(cmd,0,BUFFER);
|
||||||
|
char* argi = malloc(BUFFER);
|
||||||
|
memset(argi,0,BUFFER);
|
||||||
printf("初始化成功\n");
|
printf("初始化成功\n");
|
||||||
while(1){
|
while(1){
|
||||||
memset(input,0,BUFFER);
|
memset(cmd,0,BUFFER);
|
||||||
scanf("%s",input);
|
char* input = readline("");
|
||||||
switch(command(input)){
|
strcpy(ioput,input);
|
||||||
|
free(input);
|
||||||
|
memcpy(cmd,ioput,strcspn(ioput," "));
|
||||||
|
strcpy(argi,ioput+strcspn(ioput," ")+1);
|
||||||
|
switch(command(cmd)){
|
||||||
case receive:
|
case receive:
|
||||||
if(!listen_mode){
|
if(!listen_mode){
|
||||||
printf("接收模式已被禁用\n");
|
printf("接收模式已被禁用\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
printf("请粘贴Base64字符串:\n");
|
base64_decode(argi,buffer,NULL);
|
||||||
scanf("%s",ioput);
|
memset(argi,0,BUFFER);
|
||||||
base64_decode(ioput,buffer,NULL);
|
|
||||||
memset(ioput,0,BUFFER);
|
|
||||||
int size = text_decrypt(local_key,buffer,ioput);
|
int size = text_decrypt(local_key,buffer,ioput);
|
||||||
memset(buffer,0,BUFFER);
|
memset(buffer,0,BUFFER);
|
||||||
if(size < 0){
|
if(size < 0){
|
||||||
@@ -191,25 +207,23 @@ int main(int argn,char** argc){
|
|||||||
printf("发送模式已被禁用\n");
|
printf("发送模式已被禁用\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
printf("请输入要发送的文本或文件路径:\n");
|
|
||||||
scanf("%s",ioput);
|
|
||||||
FILE* send_file = fopen(ioput,"r");
|
FILE* send_file = fopen(ioput,"r");
|
||||||
if(strlen(ioput)>(peer_bits/8-80)){
|
if(strlen(ioput)>(peer_bits/8-80)){
|
||||||
perror("Text too long for key of peer.\n");
|
perror("Text too long for key of peer.\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(send_file!=NULL){
|
if(send_file!=NULL){
|
||||||
int send_stat = send_file_f(ioput,sockfd,peer_key,result);
|
|
||||||
fclose(send_file);
|
fclose(send_file);
|
||||||
|
int send_stat = send_file_f(argi,sockfd,peer_key,result);
|
||||||
if(send_stat!=0){
|
if(send_stat!=0){
|
||||||
perror("文件发送失败");
|
perror("文件发送失败");
|
||||||
}else{
|
}else{
|
||||||
printf("\033[32m文件发送成功: %s\033[0m\n",ioput);
|
printf("\033[32m文件发送成功: %s\033[0m\n",argi);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
size = text_encrypt(peer_key,ioput,buffer,strlen(ioput));
|
size = text_encrypt(peer_key,argi,buffer,strlen(argi));
|
||||||
memset(ioput,0,BUFFER);
|
memset(argi,0,BUFFER);
|
||||||
if(size < 0){
|
if(size < 0){
|
||||||
perror("加密失败");
|
perror("加密失败");
|
||||||
}else{
|
}else{
|
||||||
|
|||||||
Reference in New Issue
Block a user