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 7539087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:17:28+00:00 2026-05-30T07:17:28+00:00

I’m writing a C program that encrypts(based on the private key) and decrypts(based on

  • 0

I’m writing a C program that encrypts(based on the private key) and decrypts(based on the public key) text. I’m trying to do this with the OpenSSL lib. Does anyone know any good tutorial, quick starting guide or sample code? I haven’t found any decent one on the web.

  • 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-30T07:17:29+00:00Added an answer on May 30, 2026 at 7:17 am

    Here’s an example I created for encrypting a file using RSA for the asymmetric algorithm and AES-128-CBC for the symmetric algorithm, with the OpenSSL EVP functions:

    #include <stdio.h>
    #include <stdlib.h>
    
    #include <openssl/evp.h>
    #include <openssl/pem.h>
    #include <openssl/rsa.h>
    #include <openssl/err.h>
    
    #include <arpa/inet.h> /* For htonl() */
    
    int do_evp_seal(FILE *rsa_pkey_file, FILE *in_file, FILE *out_file)
    {
        int retval = 0;
        RSA *rsa_pkey = NULL;
        EVP_PKEY *pkey = EVP_PKEY_new();
        EVP_CIPHER_CTX ctx;
        unsigned char buffer[4096];
        unsigned char buffer_out[4096 + EVP_MAX_IV_LENGTH];
        size_t len;
        int len_out;
        unsigned char *ek = NULL;
        int eklen;
        uint32_t eklen_n;
        unsigned char iv[EVP_MAX_IV_LENGTH];
    
        if (!PEM_read_RSA_PUBKEY(rsa_pkey_file, &rsa_pkey, NULL, NULL))
        {
            fprintf(stderr, "Error loading RSA Public Key File.\n");
            ERR_print_errors_fp(stderr);
            retval = 2;
            goto out;
        }
    
        if (!EVP_PKEY_assign_RSA(pkey, rsa_pkey))
        {
            fprintf(stderr, "EVP_PKEY_assign_RSA: failed.\n");
            retval = 3;
            goto out;
        }
    
        EVP_CIPHER_CTX_init(&ctx);
        ek = malloc(EVP_PKEY_size(pkey));
    
        if (!EVP_SealInit(&ctx, EVP_aes_128_cbc(), &ek, &eklen, iv, &pkey, 1))
        {
            fprintf(stderr, "EVP_SealInit: failed.\n");
            retval = 3;
            goto out_free;
        }
    
        /* First we write out the encrypted key length, then the encrypted key,
         * then the iv (the IV length is fixed by the cipher we have chosen).
         */
    
        eklen_n = htonl(eklen);
        if (fwrite(&eklen_n, sizeof eklen_n, 1, out_file) != 1)
        {
            perror("output file");
            retval = 5;
            goto out_free;
        }
        if (fwrite(ek, eklen, 1, out_file) != 1)
        {
            perror("output file");
            retval = 5;
            goto out_free;
        }
        if (fwrite(iv, EVP_CIPHER_iv_length(EVP_aes_128_cbc()), 1, out_file) != 1)
        {
            perror("output file");
            retval = 5;
            goto out_free;
        }
    
        /* Now we process the input file and write the encrypted data to the
         * output file. */
    
        while ((len = fread(buffer, 1, sizeof buffer, in_file)) > 0)
        {
            if (!EVP_SealUpdate(&ctx, buffer_out, &len_out, buffer, len))
            {
                fprintf(stderr, "EVP_SealUpdate: failed.\n");
                retval = 3;
                goto out_free;
            }
    
            if (fwrite(buffer_out, len_out, 1, out_file) != 1)
            {
                perror("output file");
                retval = 5;
                goto out_free;
            }
        }
    
        if (ferror(in_file))
        {
            perror("input file");
            retval = 4;
            goto out_free;
        }
    
        if (!EVP_SealFinal(&ctx, buffer_out, &len_out))
        {
            fprintf(stderr, "EVP_SealFinal: failed.\n");
            retval = 3;
            goto out_free;
        }
    
        if (fwrite(buffer_out, len_out, 1, out_file) != 1)
        {
            perror("output file");
            retval = 5;
            goto out_free;
        }
    
        out_free:
        EVP_PKEY_free(pkey);
        free(ek);
    
        out:
        return retval;
    }
    
    int main(int argc, char *argv[])
    {
        FILE *rsa_pkey_file;
        int rv;
    
        if (argc < 2)
        {
            fprintf(stderr, "Usage: %s <PEM RSA Public Key File>\n", argv[0]);
            exit(1);
        }
    
        rsa_pkey_file = fopen(argv[1], "rb");
        if (!rsa_pkey_file)
        {
            perror(argv[1]);
            fprintf(stderr, "Error loading PEM RSA Public Key File.\n");
            exit(2);
        }
    
        rv = do_evp_seal(rsa_pkey_file, stdin, stdout);
    
        fclose(rsa_pkey_file);
        return rv;
    }
    

    And the corresponding decryption example:

    #include <stdio.h>
    #include <stdlib.h>
    
    #include <openssl/evp.h>
    #include <openssl/pem.h>
    #include <openssl/rsa.h>
    #include <openssl/err.h>
    
    #include <arpa/inet.h> /* For htonl() */
    
    int do_evp_unseal(FILE *rsa_pkey_file, FILE *in_file, FILE *out_file)
    {
        int retval = 0;
        RSA *rsa_pkey = NULL;
        EVP_PKEY *pkey = EVP_PKEY_new();
        EVP_CIPHER_CTX ctx;
        unsigned char buffer[4096];
        unsigned char buffer_out[4096 + EVP_MAX_IV_LENGTH];
        size_t len;
        int len_out;
        unsigned char *ek;
        unsigned int eklen;
        uint32_t eklen_n;
        unsigned char iv[EVP_MAX_IV_LENGTH];
    
        if (!PEM_read_RSAPrivateKey(rsa_pkey_file, &rsa_pkey, NULL, NULL))
        {
            fprintf(stderr, "Error loading RSA Private Key File.\n");
            ERR_print_errors_fp(stderr);
            retval = 2;
            goto out;
        }
    
        if (!EVP_PKEY_assign_RSA(pkey, rsa_pkey))
        {
            fprintf(stderr, "EVP_PKEY_assign_RSA: failed.\n");
            retval = 3;
            goto out;
        }
    
        EVP_CIPHER_CTX_init(&ctx);
        ek = malloc(EVP_PKEY_size(pkey));
    
        /* First need to fetch the encrypted key length, encrypted key and IV */
    
        if (fread(&eklen_n, sizeof eklen_n, 1, in_file) != 1)
        {
            perror("input file");
            retval = 4;
            goto out_free;
        }
        eklen = ntohl(eklen_n);
        if (eklen > EVP_PKEY_size(pkey))
        {
            fprintf(stderr, "Bad encrypted key length (%u > %d)\n", eklen,
                EVP_PKEY_size(pkey));
            retval = 4;
            goto out_free;
        }
        if (fread(ek, eklen, 1, in_file) != 1)
        {
            perror("input file");
            retval = 4;
            goto out_free;
        }
        if (fread(iv, EVP_CIPHER_iv_length(EVP_aes_128_cbc()), 1, in_file) != 1)
        {
            perror("input file");
            retval = 4;
            goto out_free;
        }
    
        if (!EVP_OpenInit(&ctx, EVP_aes_128_cbc(), ek, eklen, iv, pkey))
        {
            fprintf(stderr, "EVP_OpenInit: failed.\n");
            retval = 3;
            goto out_free;
        }
    
        while ((len = fread(buffer, 1, sizeof buffer, in_file)) > 0)
        {
            if (!EVP_OpenUpdate(&ctx, buffer_out, &len_out, buffer, len))
            {
                fprintf(stderr, "EVP_OpenUpdate: failed.\n");
                retval = 3;
                goto out_free;
            }
    
            if (fwrite(buffer_out, len_out, 1, out_file) != 1)
            {
                perror("output file");
                retval = 5;
                goto out_free;
            }
        }
    
        if (ferror(in_file))
        {
            perror("input file");
            retval = 4;
            goto out_free;
        }
    
        if (!EVP_OpenFinal(&ctx, buffer_out, &len_out))
        {
            fprintf(stderr, "EVP_SealFinal: failed.\n");
            retval = 3;
            goto out_free;
        }
    
        if (fwrite(buffer_out, len_out, 1, out_file) != 1)
        {
            perror("output file");
            retval = 5;
            goto out_free;
        }
    
        out_free:
        EVP_PKEY_free(pkey);
        free(ek);
    
        out:
        return retval;
    }
    
    int main(int argc, char *argv[])
    {
        FILE *rsa_pkey_file;
        int rv;
    
        if (argc < 2)
        {
            fprintf(stderr, "Usage: %s <PEM RSA Private Key File>\n", argv[0]);
            exit(1);
        }
    
        rsa_pkey_file = fopen(argv[1], "rb");
        if (!rsa_pkey_file)
        {
            perror(argv[1]);
            fprintf(stderr, "Error loading PEM RSA Private Key File.\n");
            exit(2);
        }
    
        rv = do_evp_unseal(rsa_pkey_file, stdin, stdout);
    
        fclose(rsa_pkey_file);
        return rv;
    }
    

    I think that’s fairly easy to follow. As written both commands can be used as part of a pipeline (they take input on stdin and write output to stdout).

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.