cereal icon indicating copy to clipboard operation
cereal copied to clipboard

Binary Serialization of String is still a readable string

Open cpyburn opened this issue 2 years ago • 3 comments

Shouldn't you encode the string to base64 so that it is not human readable... similar to when you binary serialize the values of xml in your example here?

https://uscilab.github.io/cereal/serialization_archives.html XML archives also support explicit binary input/output, which will encode the data as a base64 string:

#include <cereal/archives/xml.hpp>
#include <iostream>

int main()
{
  cereal::XMLOutputArchive archive( std::cout );

  int arr[] = {-1, 95, 3};
  archive.saveBinaryValue( arr, sizeof(int) * 3, "some_optional_name" );
}

which will output:

<?xml version="1.0"?>
<cereal>
  <some_optional_name>/////18AAAADAAAA</some_optional_name>
</cereal>

cpyburn avatar Sep 09 '23 12:09 cpyburn

Here is a working sample if you wanted to load it up.

#include <string.h>
using namespace std;
#include <iostream>
#include "cereal/archives/portable_binary.hpp"
#include "cereal/types/string.hpp"
#include "cereal/external/base64.hpp"
using namespace cereal::base64;
#include <fstream>

class Test
{
public:
	Test(string testString, bool humanReadable = true);

    template<class Archive>
    void serialize(Archive& archive)
    {
        // serialize things by passing them to the archive
        archive(m_testString, x, y, z);
    }

private:
	string m_testString;
    int x;
    int y;
    int z;
};

Test::Test(string testString, bool humanReadable)
{
	m_testString = testString;
    x = 12;
    y = 35493823;
    z = 15;

    if (!humanReadable)
    {
        const char* xx = m_testString.data();
        unsigned char* pixels = (unsigned char*)xx;
        m_testString = encode(pixels, sizeof(unsigned char) * m_testString.size());
        //string dcode = decode(m_testString);
    }
}

int main()
{
    // human readable
    std::ofstream file("reg.bin", std::ofstream::binary | std::ofstream::out | std::ofstream::trunc);
    {
        cereal::PortableBinaryOutputArchive arb(file);

        arb(Test("Helloworld")); 
    }

    // not human readable
    std::ofstream fileNHR("regNHR.bin", std::ofstream::binary | std::ofstream::out | std::ofstream::trunc);
    {
        cereal::PortableBinaryOutputArchive arb(fileNHR);

        arb(Test("Helloworld", false));
    }
}

cpyburn avatar Sep 09 '23 12:09 cpyburn

I don't know why you would want BASE64, its not encryption and easily reversible (https://www.base64decode.org/).

It not being initially readable does not mean it can't be made readable in no-time, the strings still stand out after writing them out as binary files as BASE64 is easily recognizable.

TheOnlyJoey avatar Sep 17 '23 23:09 TheOnlyJoey

Furthermore base64 encoding would take more space than binary encoding.

Binary does not mean unreadable, it just means "native representation".

It's normal for strings to be readable in binary files, look into the strings unix command -> it's purpose is to look for and display such strings in binary files.

f-michaut avatar Dec 10 '23 17:12 f-michaut