0

I am basically trying to make a C-String wrapper that XOR-es the strings at compile-time (so they don't show up in the file's binary) and decrypt it at run-time to be used properly. Here is my code:

class XORString
{
public:

    XORString( wchar_t* text )
    {
        this->m_Text = text;
        this->encode( );
    }

    ~XORString( void )
    {

    }

    wchar_t* m_Text = { };

    auto constexpr text( void ) -> wchar_t*
    {
        return this->decode( );
    }

    auto constexpr length( void ) -> int
    {
        int i = { };

        while ( '\0' != this->m_Text [ i ] )
        {
            ++i;
        }

        return i;
    }

private:

    auto constexpr copy( wchar_t** v1, wchar_t** v2 ) -> bool
    {
        int v1_len = { };
        int v2_len = { };

        while ( '\0' != v1 [ v1_len ] )
        {
            ++v1_len;
        }

        while ( '\0' != v2 [ v2_len ] )
        {
            ++v2_len;
        }

        for ( int i = { }; v2 [ i ] != '\0'; ++i )
        {
            v1 [ i ] = v2 [ i ];
        }

        return true;
    }

public:

    auto constexpr encode( void ) noexcept -> void
    {
        wchar_t* new_text = { };
        this->copy( &new_text, &this->m_Text );

        for ( int i = { }; i < this->length( ); ++i )
        {
            int r = random_int( ) % 100;
            new_text [ i ] ^= static_cast<wchar_t>( r );
        }

        this->m_Text = new_text;
    }

    auto constexpr decode( void ) -> wchar_t*
    {
        wchar_t* new_text = { };
        this->copy( &new_text, &this->m_Text );

        for ( int i = { }; i < this->length( ); ++i )
        {
            int r = random_int( ) % 100;
            new_text [ i ] ^= static_cast<wchar_t>( r );
        }

        return new_text;
    }
};

It's supposed to be used like this:

XORString lpString = (wchar_t*)L"My string";

I'm using wchar_t* instead of const wchar_t* since I don't know how I would even attempt to use the string in my encoding/decoding if it's a constant, but you can comment on this too.

The error:

I get the error in the encoding function. It says

Exception thrown: write access violation. new_text was 0x Random address.

I saw some codes that do this online but they are way too complicated to the point where I can't even really understand what is going on and I wanted to make my own. I'd appreciate any kind of help, thanks!

  • You are effectively trying to modify string literal. This is not allowed, as those literals are often put in read-only section. The fact that you had to cast, shows that it can't be done this way. – SergeyA Jun 12 '18 at 14:29
  • 1
    In `wchar_t* new_text = { }; this->copy( &new_text, &this->m_Text );` where do you allocate memory to store the characters in? – NathanOliver Jun 12 '18 at 14:31
  • I see a lot of working with pointers but I never see when you allocate memory, how you manage ownership of memory (constructor takes the pointer and what about ownership of data?). In addition using C++ implies you have `std::string` which is perfectly fine for your purpose. And then, if you use `random_int()%100` to encode the data how can you use the same key for decoding? – Jack Jun 12 '18 at 14:31
  • @JaneDoe How do you suppose, we explain that, without knowing what those "others" are, or how their code looks like? Why can't you ask the authors of such codes, for explanation? – Algirdas Preidžius Jun 12 '18 at 14:33
  • @everyone I know about those, but the encoding is supposed to happen during compilation time and how would I allocate memory for that? I has to be done another way. How did the others do it? EDIT: How exactly would I use std::string for this? – Jane Doe Jun 12 '18 at 14:33
  • Here it is, second answer: https://stackoverflow.com/questions/7270473/compile-time-string-encryption . You can take a look at all of them, though – Jane Doe Jun 12 '18 at 14:34
  • You should always provide a complete, compilable example which allows others to reproduce the problem - a [MCVE]. – davmac Jun 14 '18 at 11:53

0 Answers0