int do_crypt(char *outfile)
{
unsigned char outbuf[];
int outlen, tmplen;
/* Bogus key and IV: we'd normally set these from
* another source.
*/
unsigned char key[] = {,,,,,,,,,,,,,,,};
unsigned char iv[] = {,,,,,,,};
char intext[] = "Some Crypto Text";
EVP_CIPHER_CTX *ctx;
FILE *out; ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_idea_cbc(), NULL, key, iv); if(!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext)))
{
/* Error */
return ;
}
/* Buffer passed to EVP_EncryptFinal() must be after data just
* encrypted to avoid overwriting it.
*/
if(!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))
{
/* Error */
return ;
}
outlen += tmplen;
EVP_CIPHER_CTX_free(ctx);
/* Need binary mode for fopen because encrypted data is
* binary data. Also cannot use strlen() on it because
* it won't be null terminated and may contain embedded
* nulls.
*/
out = fopen(outfile, "wb");
fwrite(outbuf, , outlen, out);
fclose(out);
return ;
}
  int do_crypt(FILE *in, FILE *out, int do_encrypt)
{
/* Allow enough space in output buffer for additional block */
unsigned char inbuf[], outbuf[ + EVP_MAX_BLOCK_LENGTH];
int inlen, outlen;
EVP_CIPHER_CTX *ctx;
/* Bogus key and IV: we'd normally set these from
* another source.
*/
unsigned char key[] = "0123456789abcdeF";
unsigned char iv[] = ""; /* Don't set key or IV right away; we want to check lengths */
ctx = EVP_CIPHER_CTX_new();
EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, NULL, NULL,
do_encrypt);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == );
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == ); /* Now we can set key and IV */
EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt); for(;;)
{
inlen = fread(inbuf, , , in);
if (inlen <= ) break;
if(!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen))
{
/* Error */
EVP_CIPHER_CTX_free(ctx);
return ;
}
fwrite(outbuf, , outlen, out);
}
if(!EVP_CipherFinal_ex(ctx, outbuf, &outlen))
{
/* Error */
EVP_CIPHER_CTX_free(ctx);
return ;
}
fwrite(outbuf, , outlen, out); EVP_CIPHER_CTX_free(ctx);
return ;
}

// openssl.org/docs/man1.1.0/man3/EVP_EncryptInit_ex.html

05-11 22:52