Skip to content

Commit 179f527

Browse files
committed
v1.3
1.send PK when register 2.use nonce to setup session
1 parent 9ddf997 commit 179f527

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+679
-32
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ netdisk/
5656
core
5757
!client.exe
5858
client/downloads
59+
server_rsa.key
60+
client_rsa.key
61+
client_rsa_pub.key

bin/Makefile

Lines changed: 0 additions & 9 deletions
This file was deleted.

client.exe

-21.9 KB
Binary file not shown.

client/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SOUR:=$(wildcard bin/*.c)
2+
ELF:=client
3+
OBJS:=$(SOUR:%.c=%.o)
4+
$(ELF):$(OBJS)
5+
gcc $^ -o $@ -lpthread -lcrypto
6+
clean:
7+
rm -rf $(ELF) $(OBJS)

client/client.c renamed to client/bin/client.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "client.h"
1+
#include "../include/client.h"
22

33
int connect_server(int* socketFd, const char* ip, const char* port)
44
{
@@ -20,10 +20,14 @@ int connect_server(int* socketFd, const char* ip, const char* port)
2020
int login_page(int flag)
2121
{
2222
system("clear");
23-
if (flag)
23+
if (flag == 1)
2424
{
2525
printf("register success\n");
2626
}
27+
else if (flag == -1)
28+
{
29+
printf("register failed\n");
30+
}
2731
printf("Please enter a num to continue... \n");
2832
printf("1.\tLogin\n");
2933
printf("2.\tRegister\n");

client/bin/crypto.c

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#include "../include/crypto.h"
2+
3+
int rsa_generate_key()
4+
{
5+
int ret;
6+
ret = access("client_rsa.key", F_OK);
7+
if (ret)
8+
{
9+
system("openssl genrsa -out client_rsa.key 2048");
10+
}
11+
ret = access("client_rsa.key", F_OK);
12+
if (ret)
13+
{
14+
printf("key generation fail, check if openssl is installed\n");
15+
return -1;
16+
}
17+
ret = access("client_rsa_pub.key", F_OK);
18+
if (ret)
19+
{
20+
system("openssl rsa -in client_rsa.key -pubout -out client_rsa_pub.key");
21+
}
22+
ret = access("client_rsa_pub.key", F_OK);
23+
if (ret)
24+
{
25+
printf("key generation fail, check if openssl is installed\n");
26+
return -1;
27+
}
28+
return 0;
29+
}
30+
31+
char* rsa_encrypt(char* str)
32+
{
33+
int ret;
34+
char* en_str;;
35+
FILE* fp;
36+
37+
fp = fopen("server_rsa_pub.key", "rb");
38+
if (fp == NULL)
39+
{
40+
printf("server_rsa_pub.key not found\n");
41+
return NULL;
42+
}
43+
44+
RSA* rsa;
45+
46+
rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
47+
if (rsa == NULL)
48+
{
49+
printf("rsa_pub_key read failed\n");
50+
fclose(fp);
51+
RSA_free(rsa);
52+
return NULL;
53+
}
54+
55+
int len = strlen(str);
56+
en_str = (char*)calloc(256, sizeof(char));
57+
ret = RSA_public_encrypt(len, (unsigned char*)str, (unsigned char*)en_str, rsa, RSA_PKCS1_PADDING);
58+
if (ret == -1)
59+
{
60+
printf("rsa_encrypt failed\n");
61+
fclose(fp);
62+
RSA_free(rsa);
63+
return NULL;
64+
}
65+
66+
fclose(fp);
67+
RSA_free(rsa);
68+
return en_str;
69+
}
70+
71+
char* rsa_sign(char* str)
72+
{
73+
int ret;
74+
char* en_str;;
75+
FILE* fp;
76+
77+
fp = fopen("client_rsa.key", "rb");
78+
if (fp == NULL)
79+
{
80+
printf("client_rsa.key not found\n");
81+
return NULL;
82+
}
83+
84+
RSA* rsa;
85+
86+
rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
87+
if (rsa == NULL)
88+
{
89+
printf("rsa_private_key read failed\n");
90+
fclose(fp);
91+
RSA_free(rsa);
92+
return NULL;
93+
}
94+
95+
int len = strlen(str);
96+
en_str = (char*)calloc(RSA_EN_LEN, sizeof(char));
97+
ret = RSA_private_encrypt(len, (unsigned char*)str, (unsigned char*)en_str, rsa, RSA_PKCS1_PADDING);
98+
if (ret == -1)
99+
{
100+
printf("rsa sign failed\n");
101+
fclose(fp);
102+
RSA_free(rsa);
103+
return NULL;
104+
}
105+
106+
fclose(fp);
107+
RSA_free(rsa);
108+
return en_str;
109+
}
110+
char* rsa_decrypt(char* str)
111+
{
112+
113+
}
114+
115+
char* rsa_verify(char* str)
116+
{
117+
int ret;
118+
char* de_str;
119+
120+
FILE* fp = fopen("server_rsa_pub.key", "rb");
121+
if (fp == NULL)
122+
{
123+
printf("server_rsa_pub.key not found\n");
124+
return NULL;
125+
}
126+
127+
RSA* rsa;
128+
rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
129+
if (rsa == NULL)
130+
{
131+
printf("server_pub_key read failed\n");
132+
fclose(fp);
133+
RSA_free(rsa);
134+
return NULL;
135+
}
136+
137+
de_str = (char*)calloc(RSA_DE_LEN, sizeof(char));
138+
ret = RSA_public_decrypt(RSA_EN_LEN, (unsigned char*)str, (unsigned char*)de_str, rsa, RSA_PKCS1_PADDING);
139+
if (ret == -1)
140+
{
141+
printf("decrption failed\n");
142+
fclose(fp);
143+
RSA_free(rsa);
144+
return NULL;
145+
}
146+
147+
fclose(fp);
148+
RSA_free(rsa);
149+
return de_str;
150+
}

client/main.c renamed to client/bin/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "head.h"
2-
#include "transmission.h"
3-
#include "client.h"
1+
#include "../include/head.h"
2+
#include "../include/transmission.h"
3+
#include "../include/client.h"
44

55
int main(int argc, char** argv)
66
{
@@ -40,9 +40,10 @@ int main(int argc, char** argv)
4040
}
4141
else if (ret == '2')
4242
{
43-
user_signup(&socketFd, argv[1], argv[2], user_name, &data);
43+
ret = user_signup(&socketFd, argv[1], argv[2], user_name, &data);
4444
if (ret == -1)
4545
{
46+
regi_flag = -1;
4647
continue;
4748
}
4849
regi_flag = 1;

client/md5.c renamed to client/bin/md5.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "md5.h"
1+
#include "../include/md5.h"
22

33
unsigned char PADDING[] =
44
{

0 commit comments

Comments
 (0)