51 lines
1.4 KiB
C
51 lines
1.4 KiB
C
// Compile this code via gcc -o openssl_encrypt openssl_encrypt.c -lssl -lcrypto
|
|
|
|
#include <openssl/evp.h>
|
|
#include <openssl/aes.h>
|
|
#include <openssl/err.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
void handleErrors(void) {
|
|
ERR_print_errors_fp(stderr);
|
|
abort();
|
|
}
|
|
|
|
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
|
|
unsigned char *iv, unsigned char *ciphertext) {
|
|
EVP_CIPHER_CTX *ctx;
|
|
int len;
|
|
int ciphertext_len;
|
|
|
|
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, plaintext, plaintext_len))
|
|
handleErrors();
|
|
ciphertext_len = len;
|
|
|
|
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
|
|
ciphertext_len += len;
|
|
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
|
|
return ciphertext_len;
|
|
}
|
|
|
|
int main(void) {
|
|
unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
|
|
unsigned char *iv = (unsigned char *)"0123456789012345";
|
|
unsigned char *plaintext = (unsigned char *)"This is a secret message";
|
|
unsigned char ciphertext[128];
|
|
|
|
int ciphertext_len = encrypt(plaintext, strlen((char *)plaintext), key, iv, ciphertext);
|
|
|
|
printf("PLaintext: %s\n", plaintext);
|
|
printf("Ciphertext:\n");
|
|
BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);
|
|
|
|
return 0;
|
|
}
|