-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
144 lines (120 loc) · 4.28 KB
/
client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#define maxLength 3000
//CLIENT
unsigned char iv[16] = {
0x2b, 0x7e, 0x15, 0x16,
0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88,
0x09, 0xcf, 0x4f, 0x3c
};
unsigned char key[32] = {
0x2b, 0x7e, 0x15, 0x16,
0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88,
0x09, 0xcf, 0x4f, 0x3c,
0x2b, 0x7e, 0x15, 0x16,
0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88,
0x09, 0xcf, 0x4f, 0x3c
};
unsigned char tag[16] = {
0x1a, 0x47, 0xf0, 0x10, 0x33, 0x6c, 0x14, 0x08,
0x14, 0xac, 0x38, 0x4f, 0x98, 0x4a, 0xd3, 0xdf
};
unsigned char aad[] = "Additional Authenticated Data";
int gcm_encrypt(unsigned char *plaintext, int plaintext_len,
unsigned char *aad, int aad_len,
unsigned char *key,
unsigned char *iv, int iv_len,
unsigned char *ciphertext,
unsigned char *tag)
{
EVP_CIPHER_CTX *ctx;
int len = 0;
int ciphertext_len = 0;
/* Create and initialize the context */
ctx = EVP_CIPHER_CTX_new();
/* Initialize the encryption operation */
EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
/* Set IV length */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL);
/* Initialize key and IV */
EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv);
/* Provide any AAD data */
EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len);
/* Provide the message to be encrypted and obtain the encrypted output */
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
ciphertext_len = len;
/* Finalize the encryption and generate the authentication tag */
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len;
/* Get the tag */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag);
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int main(void)
{
//VARIABLE DEFINITION
int PORT = 8080;
const char *IP = "127.0.0.1";
//CREATING SOCKET
sockaddr_in clientSocketAddress;
memset(&clientSocketAddress, 0, sizeof(clientSocketAddress));
clientSocketAddress.sin_family = AF_INET;
clientSocketAddress.sin_addr.s_addr = inet_addr(IP);
clientSocketAddress.sin_port = htons(PORT);
int client = socket(AF_INET, SOCK_STREAM, 0);
//CONNECTING TO SERVER
if (connect(client,(sockaddr*) &clientSocketAddress, sizeof(clientSocketAddress))>=0){
printf("Successfully connected to server\n");
}
//DECLARING BUFFERS
unsigned char serverMessage[maxLength];
unsigned char plaintextBuffer[maxLength];
unsigned char encryptedBuffer[maxLength];
while(true)
{
//SENDING MESSAGE TO SERVER FROM COMMAND LINE
printf("Client: ");
std::string input;
std::getline(std::cin, input);
//PUTTING INTO BUFFER AND ENCRYPTING
memcpy(plaintextBuffer, input.c_str(), input.length() + 1);
int ciphertextLength = gcm_encrypt(plaintextBuffer, input.length(), aad, sizeof(aad),key, iv,sizeof(iv), encryptedBuffer, tag);
//PRINTING CIPHERTEXT AND TAG
printf("Ciphertext: ");
for (int i = 0; i < ciphertextLength; ++i) {
printf("%02x", encryptedBuffer[i]);
}
printf("\nTag: ");
for (int i = 0; i < 16; ++i) {
printf("%02x", tag[i]);
}
printf("\n");
//SENDING CIPHERTEXT AND TAG
send(client, (char*)encryptedBuffer, ciphertextLength, 0);
send(client, tag, 16, 0);
printf("Waiting for server response\n");
// CLEARING BUFFERS FOR NEXT MESSAGE
memset(&serverMessage, 0, sizeof(serverMessage));
memset(&plaintextBuffer, 0, sizeof(plaintextBuffer));
memset(&encryptedBuffer, 0, sizeof(encryptedBuffer));
//RECEIVE AND PRINT SERVER RESPONSE
recv(client, (char*)&serverMessage, sizeof(serverMessage), 0);
printf("Server: %s\n", serverMessage);
}
close(client);
printf("Socket closed\n");
return 0;
}