Perhaps the easiest way is to serialize your structure into a string and then encrypt the string. For instance:
std::ostringstream buffer; buffer << a_record.name << "\n" << a_record.location << "\n" << a_record.salary; encode(buffer.str().c_str(), buffer.str().length(), );
If it were me, I would probably write encode (or at least a wrapper for it) to accept input (and probably produce output) into a vector, string or stream.
If you want ambitiously, there are other possibilities. First of all, @MooingDuck raises a good point that it is often worth overloading operator<< for a class, rather than working with individual elements all the time. Usually this will be a small function similar to the one described above:
std::ostream &operator<<(std::ostream &os, record const &r) { return os << r.name << "\n" << r.location << "\n" << r.salary; }
Using this, you simply have:
std::ostringstream os; os << a_record; encode(os.str().c_str(), os.str().length(), );
Secondly, if you want to become very ambitious, you can put encryption in (for example) the codecvt facet codecvt that you can automatically encrypt all data when writing to the stream and decrypt it as you read it again. Another possibility is to instead encrypt to the streambuf filtering streambuf . The codecvt is probably a method that should theoretically be preferred, but streambuf almost certainly easier to implement, with less unrelated โstuffโ involved.
source share