done
This commit is contained in:
52
client.cpp
52
client.cpp
@@ -34,6 +34,25 @@ void load_env(const std::string& path) {
|
||||
}
|
||||
}
|
||||
|
||||
// hex to byte helper function
|
||||
int hex_to_bytes_upper(const char* hex, unsigned char* out, size_t out_size)
|
||||
{
|
||||
size_t i = 0;
|
||||
while (hex[0] && hex[1]) {
|
||||
if (i >= out_size) return -1;
|
||||
unsigned char h = hex[0];
|
||||
unsigned char l = hex[1];
|
||||
int hi = (h <= '9') ? (h - '0') : (h - 'A' + 10);
|
||||
int lo = (l <= '9') ? (l - '0') : (l - 'A' + 10);
|
||||
// minimal sanity check
|
||||
if (hi < 0 || hi > 15 || lo < 0 || lo > 15)
|
||||
return -1;
|
||||
out[i++] = (unsigned char)((hi << 4) | lo);
|
||||
hex += 2;
|
||||
}
|
||||
return (int)i;
|
||||
}
|
||||
|
||||
// steam cipher encryption function
|
||||
int stream_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) {
|
||||
/* Declare cipher context */
|
||||
@@ -43,6 +62,9 @@ int stream_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *k
|
||||
|
||||
/* Create and initialise the context */
|
||||
ctx = EVP_CIPHER_CTX_new();
|
||||
if (!ctx) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
}
|
||||
|
||||
/* Initialise the encryption operation. */
|
||||
// choice for aes_256 ref: https://stackoverflow.com/questions/1220751/how-to-choose-an-aes-encryption-mode-cbc-ecb-ctr-ocb-cfb
|
||||
@@ -63,7 +85,7 @@ int stream_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *k
|
||||
/* Clean up */
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
return ciphertext_len;
|
||||
return len + ciphertext_len;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
@@ -80,7 +102,16 @@ int main(void){
|
||||
// encryption parameters
|
||||
const char *inital_vector = std::getenv("INITIAL_VECTOR");
|
||||
const char *secret_key = std::getenv("SECRET_KEY");
|
||||
unsigned char key_bytes[32], iv_bytes[16];
|
||||
|
||||
int key_len = hex_to_bytes_upper(secret_key, key_bytes, sizeof(key_bytes));
|
||||
int iv_len = hex_to_bytes_upper(inital_vector, iv_bytes, sizeof(iv_bytes));
|
||||
|
||||
if (key_len != 32 || iv_len != 16) {
|
||||
fprintf(stderr, "Invalid key/IV size for AES-256\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create socket:
|
||||
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (client_socket == -1) {
|
||||
@@ -121,14 +152,23 @@ int main(void){
|
||||
char buffer[2048];
|
||||
std::snprintf(buffer, sizeof(buffer), "%s%s", custom_message, client_message);
|
||||
|
||||
// encrypt message
|
||||
char ciphertext[2048];
|
||||
int ciphertext_len = stream_encrypt((unsigned char*)buffer, sizeof(buffer), (unsigned char*)secret_key, (unsigned char*)inital_vector, (unsigned char*)ciphertext);
|
||||
unsigned char ciphertext[2048];
|
||||
|
||||
send(client_socket, ciphertext, ciphertext_len, 0);
|
||||
int plaintext_len = (int)strlen(buffer);
|
||||
int ciphertext_len = stream_encrypt((unsigned char*)buffer, plaintext_len, key_bytes, iv_bytes, ciphertext);
|
||||
if (ciphertext_len <= 0) {
|
||||
printf("encrypt failed or produced empty ciphertext_len=%d\n", ciphertext_len);
|
||||
break;
|
||||
}
|
||||
|
||||
ssize_t sent = send(client_socket, ciphertext, (size_t)ciphertext_len, 0);
|
||||
if (sent <= 0) { perror("No message sent to server"); break; }
|
||||
|
||||
// Receive the server's response:
|
||||
recv(client_socket, server_message, sizeof(server_message), 0);
|
||||
// add terminator for string
|
||||
ssize_t recvd = recv(client_socket, server_message, sizeof(server_message) - 1, 0);
|
||||
if (recvd <= 0) { perror("No message received from server"); break; }
|
||||
server_message[recvd] = '\0';
|
||||
|
||||
printf("Server's response: %s\n", server_message);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user