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!