Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3486204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:00:02+00:00 2026-05-18T11:00:02+00:00

I am currently writing a linux application in C that reads from a configuration

  • 0

I am currently writing a linux application in C that reads from a configuration file. This configuration file contains some data that I would like to encrypt so it is not plain text. I have spent hours researching this and have not found a viable solution. Since the application will need to read from the configuration I will need to be able to encrypt it and decrypt it on the fly. So far from research I really like openSSL crypto library. I know from the command line you can do:

openssl enc -aes-256-cbc -salt -in file.txt -out file.enc

If anyone can provide an example of how I can do this in C, it would be most appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:00:03+00:00Added an answer on May 18, 2026 at 11:00 am

    You should have a look at the O’Reilly-Book. There are a couple of examples on how to encrypt things. Unfortunately the most are for Network-Encryption.

    I found an example in the book, but didnt test it:

    #include <openssl/evp.h>
    
    int main(int argc,  char *argv[])
    {
        EVP_CIPHER_CTX  ctx;
        char            key[EVP_MAX_KEY_LENGTH];
        char            iv[EVP_MAX_IV_LENGTH];
        char            *ct, *out;
        char            final[EVP_MAX_BLOCK_LENGTH];
        char            str[] = "123456789abcdef";
        int             i;
        if (!seed_prng())
        {
        printf("Fatal Error!  Unable to seed the PRNG!\n");
        abort();
        }
        select_random_key(key, EVP_MAX_KEY_LENGTH);
        select_random_iv(iv, EVP_MAX_IV_LENGTH);
        EVP_EncryptInit(&ctx, EVP_bf_cbc(), key, iv);
        ct = encrypt_example(&ctx, str, strlen(str), &i);
        printf("Ciphertext is %d bytes.\n", i);
        EVP_DecryptInit(&ctx, EVP_bf_cbc(), key, iv);
        out = decrypt_example(&ctx, ct, 8);
        printf("Decrypted: >>%s<<\n", out);
        out = decrypt_example(&ctx, ct + 8, 8);
        printf("Decrypted: >>%s<<\n", out);
        if (!EVP_DecryptFinal(&ctx, final, &i))
        {
        printf("Padding incorrect.\n");
        abort();
        }
        final[i] = 0;
        printf("Decrypted: >>%s<<\n", final);
    }
    
    char *encrypt_example(EVP_CIPHER_CTX *ctx, char *data, int inl, int *rb) 
    {
        char *ret;
        int i, tmp, ol;
        ol = 0;
        ret = (char *)malloc(inl + EVP_CIPHER_CTX_block_size(ctx));
        for (i = 0; i < inl / 100; i++)
        {
        EVP_EncryptUpdate(ctx, &ret[ol], &tmp, &data[ol], 100);
        ol += tmp;
        }
        if (inl % 100)
        {
        EVP_EncryptUpdate(ctx, &ret[ol], &tmp, &data[ol], inl%100);
        ol += tmp;
        }
        EVP_EncryptFinal(ctx, &ret[ol], &tmp);
        *rb = ol + tmp;
        return ret;
    }
    
    char *decrypt_example(EVP_CIPHER_CTX *ctx, char *ct, int inl)
    {
        /* We're going to null-terminate the plaintext under the assumption it's
         * non-null terminated ASCII text. The null can be ignored otherwise.
         */
        char *pt = (char *)malloc(inl + EVP_CIPHER_CTX_block_size(ctx) + 1);
        int ol;
        EVP_DecryptUpdate(ctx, pt, &ol, ct, inl);
        if (!ol) /* there's no block to decrypt */
        {
        free(pt);
        return NULL;
        }
        pt[ol] = 0;
        return pt;
    }
    

    Hope this will help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an application that on some stage performs low-level disk operations in Linux
I'm writing an application that is currently a pure QT4 app. It is designed
We are writing an application that compiles with both gcc and Visual C++. Some
I am currently writing a simple, timer-based mini app in C# that performs an
I'm currently writing an ASP.Net app from the UI down. I'm implementing an MVP
I'm currently writing an interface to allow applications to send exception data to a
I'm currently writing a simple .sh script to parse an Exim log file for
I am currently writing a little bootstrap code for a service that can be
I'm working on an issue tracking system and would like the application to handle
I'm currently writing a Java application to be used with a Windows-Machine authed with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.