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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:16:01+00:00 2026-05-29T06:16:01+00:00

I would like to use OpenSSL’s cms library to encrypt a file with a

  • 0

I would like to use OpenSSL’s cms library to encrypt a file with a certain certificate using the CMS_encrypt() method. (I guess this should be the right one)
Does anyone has a small sample about using CMS_encrypt()?

Thanks

  • 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-29T06:16:02+00:00Added an answer on May 29, 2026 at 6:16 am

    I’ve found a demo in C in the openssl download folder in openssl…/demos/cms/cms_enc.c

    With the following code (property of openssl):

        /* Simple S/MIME encrypt example */
    #include <openssl/pem.h>
    #include <openssl/cms.h>
    #include <openssl/err.h>
    
    int main(int argc, char **argv)
        {
        BIO *in = NULL, *out = NULL, *tbio = NULL;
        X509 *rcert = NULL;
        STACK_OF(X509) *recips = NULL;
        CMS_ContentInfo *cms = NULL;
        int ret = 1;
    
        /*
         * On OpenSSL 1.0.0 and later only:
         * for streaming set CMS_STREAM
         */
        int flags = CMS_STREAM;
    
        OpenSSL_add_all_algorithms();
        ERR_load_crypto_strings();
    
        /* Read in recipient certificate */
        tbio = BIO_new_file("signer.pem", "r");
    
        if (!tbio)
            goto err;
    
        rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
    
        if (!rcert)
            goto err;
    
        /* Create recipient STACK and add recipient cert to it */
        recips = sk_X509_new_null();
    
        if (!recips || !sk_X509_push(recips, rcert))
            goto err;
    
        /* sk_X509_pop_free will free up recipient STACK and its contents
         * so set rcert to NULL so it isn't freed up twice.
         */
        rcert = NULL;
    
        /* Open content being encrypted */
    
        in = BIO_new_file("encr.txt", "r");
    
        if (!in)
            goto err;
    
        /* encrypt content */
        cms = CMS_encrypt(recips, in, EVP_des_ede3_cbc(), flags);
    
        if (!cms)
            goto err;
    
        out = BIO_new_file("smencr.txt", "w");
        if (!out)
            goto err;
    
        /* Write out S/MIME message */
        if (!SMIME_write_CMS(out, cms, in, flags))
            goto err;
    
        ret = 0;
    
        err:
    
        if (ret)
            {
            fprintf(stderr, "Error Encrypting Data\n");
            ERR_print_errors_fp(stderr);
            }
    
        if (cms)
            CMS_ContentInfo_free(cms);
        if (rcert)
            X509_free(rcert);
        if (recips)
            sk_X509_pop_free(recips, X509_free);
    
        if (in)
            BIO_free(in);
        if (out)
            BIO_free(out);
        if (tbio)
            BIO_free(tbio);
    
        return ret;
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using AdoNetAppender (SQL server) in my asp.net application and would like use
I'd like to use the OpenSSL library to decrypt some AES data. The code
I would like use from Spring.NET Aspect library Logging aspect together with log4Net. I
I would like to use a language that I am familiar with - Java,
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
I would like to use a component that exposes the datasource property, but instead
I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to use javascript to develop general-purpose GUI applications. Initially these are
I would like to use my laptop as a web development (PHP, Python, etc.)

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.