Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Serialization of String is still a readable string #799

Open
cpyburn opened this issue Sep 9, 2023 · 3 comments
Open

Binary Serialization of String is still a readable string #799

cpyburn opened this issue Sep 9, 2023 · 3 comments

Comments

@cpyburn
Copy link

cpyburn commented Sep 9, 2023

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
Copy link
Author

cpyburn commented Sep 9, 2023

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));
    }
}

@TheOnlyJoey
Copy link

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.

@f-michaut
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants