Spaces:
Runtime error
Runtime error
| 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; | |
| } | |