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

  • Home
  • SEARCH
  • 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 601523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:43:29+00:00 2026-05-13T16:43:29+00:00

This is two questions in one but hopefully trivial to a C++ developer. How

  • 0

This is two questions in one but hopefully trivial to a C++ developer.

  1. How can I seralize an object so that I can write it to disk and retrieve it later in C++ or if this is the wrong keyword how can I write an object as a binary stream and recreate it later? Can I use inheritance to make a hierarchy of classes serializable?
  2. what’s the simplest way to encrypt / decrypt a stream of binary data given that I have an encryption key;

    const vector &encryption_key

This is a proof of concept so the strength or infallibility of the encryption is less important than the code being simple and easy to explain.

I can expand on either part of the question as required, as you’ve probably guessed I need to persist some data to the hard disk in files and retrieve it later in another run of the application, the files are large and this is my way of caching data retrieved over the network.

Thanks,

Gav

  • 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-13T16:43:29+00:00Added an answer on May 13, 2026 at 4:43 pm

    Boost.Serialization is probably the best option for doing this in C++. If you want to save binary data you need to create a boost::archive::binary_oarchive and associate it to your file:

    std::ofstream ofs("my_file.dat");
    boost::archive::binary_oarchive oarch(ofs);
    

    Any class you want to serialize must have a member function serialize with a special signature that the library can understand. For example:

    class Foo
    {
       int i;
       Baz baz;
    
       template<class Archive>
       void serialize(Archive &ar, unsigned int version) {
           ar & i;
           ar & baz; // Baz must be serializable
       }
    };
    

    Note that there’s built-in support for versioning but that’s a more advanced topic.

    Saving objects of your class to the binary archive is then very easy:

    Foo foo;
    oarch << foo;  // Serializes "foo"
    

    The cool thing about Boost.Serialization is that same member function is used to deserialize the object. The only difference is that now you use an input archive:

    std::ifstream ifs("my_file.dat");
    boost::archive::binary_iarchive iarch(ofs);
    Foo foo;  
    iarch >> foo; // Deserializes "foo"
    

    As for the encryption part, the Botan library seems to be pretty mature an its C++ unlike OpenSSL that it’s C and so a bit painful to use.
    This is how I think you can do serialization + encryption under the same workflow (deserialization would be analogous):

    1. You associate your archive to an in-memory string instead of to a file:

      std::ostringstream oss;
      boost::archive::binary_oarchive oarch(oss);
      

      Everything you write to the archive will be stored in the string.

    2. You serialize your objects like you did before:

      Foo foo;
      oarch << foo;  // Serializes "foo" (data goes to the string)
      
    3. You use the Botan library to encrypt your string. Don’t take this too literally but it should be something like:

      a) create a Botan memory data source associated to your string (the oss object).

      b) create a Botan data sink associated to the file where you want to write (“myfile.dat”).

      c) create an encoder that suits your needs

      d) call the encode function as in encode(source, sink);

    EDIT: Changed crypto recommendantion from OpenSSL to Botan.

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

Sidebar

Related Questions

Two questions. 1) I understand this must be an expected outcome but maybe someone
Background This question is in two parts. I have a one-way WCF operation hosted
This is a follow-up to two questions I asked a week or so back.
This might be a bit of a silly question but; If I have two
This post is actually divided in two questions: Which kinds of associations classes/interfaces usually
This is a two part question. A dumb technical query and a broader query
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This question has been discussed in two blog posts ( http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/ , http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/ ),
This is a somewhat low-level question. In x86 assembly there are two SSE instructions:
This is a follow up question . So, Java store's integers in two's-complements and

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.