Spaces:
Paused
Paused
File size: 2,297 Bytes
55c3ad6 | 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 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <curl/curl.h>
#define AES_256_KEY_SIZE 32
#define AES_256_IV_SIZE 16
void handleErrors(void) {
ERR_print_errors_fp(stderr);
abort();
}
void encryptData(const char *plaintext, unsigned char **ciphertext, int *ciphertext_len, unsigned char *key, unsigned char *iv) {
EVP_CIPHER_CTX *ctx;
int len;
*ciphertext = (unsigned char *)malloc(strlen(plaintext) + AES_256_IV_SIZE);
if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) handleErrors();
if (1 != EVP_EncryptUpdate(ctx, *ciphertext, &len, (unsigned char *)plaintext, strlen(plaintext))) handleErrors();
*ciphertext_len = len;
if (1 != EVP_EncryptFinal_ex(ctx, *ciphertext + len, &len)) handleErrors();
*ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx);
}
void establishSecureConnection() {
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
printf("Secure connection established successfully.\n");
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
int main() {
const char *plaintext = "Sensitive Data";
unsigned char *ciphertext;
int ciphertext_len;
unsigned char key[AES_256_KEY_SIZE];
unsigned char iv[AES_256_IV_SIZE];
if (!RAND_bytes(key, sizeof(key)) || !RAND_bytes(iv, sizeof(iv))) {
fprintf(stderr, "RAND_bytes failed\n");
return 1;
}
encryptData(plaintext, &ciphertext, &ciphertext_len, key, iv);
if (ciphertext) {
printf("Encrypted Data: ");
for (int i = 0; i < ciphertext_len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
free(ciphertext);
}
establishSecureConnection();
return 0;
}
|