0

I'm attempting to iterate a string but seem to be getting a weird access violation, The problem is caused when passing a value returned from JSON.stringify because the data seems to be read only, upon many attempts to use hacky methods to solve the issue I have not been successful in doing so.

I've tried copying the data, manually iterating the string and copying the string over to another variable, but the issue still remains the same no matter what I've tried.

The code below works flawlessly when a protected piece of data is not passed

    xor_swap(keys, data)
    {
        for(var i = 0; i < data.length; i++)
            data[i] ^= this.xor_key_exchange(keys, i);

        return data.toString('utf8');
    }

How ever when applying a parameter (for instance JSON.stringify), The data becomes protected and no matter what I seem to do the data seems to not be modifiable.

     var enc = this.xor_swap(keys,JSON.stringify(data));

Please note that the input is completely correct, I have tested this many times.

Of-course the expected output is that the string should be iteratable, and after speaking to a few people who are very experienced in nodejs they can't seem to see why this problem is being caused.

I am not using strict-mode for anyone asking.

Thanks to anyone who can help me with this problem

0

2 Answers 2

1

In Javascript string is immutable. You cannot do the following

var a = 'hello';
a[1] = 'a' // try change 'e' to 'a', not possible
console.log(a)
JSON.stringify() returns a string which by definition is immutable.

So, this assignment here is invalid

data[i] ^= this.xor_key_exchange(keys, i);
Sign up to request clarification or add additional context in comments.

Comments

0

thanks for your reply, I noticed that the conversion to a string is indeed immutable.

So I ended up solving this problem by simply converting the string to a buffer, completing my xor and then just converting the buffer directly back to a utf8 string again.

    xor_swap(keys, data)
    {
        var buf = Buffer.from(data);

        for(var i = 0; i < buf.length; i++)
            buf[i] ^= Math.abs(this.xor_key_exchange(keys, i));

        return buf.toString('utf8');
    }

Thank you very much for your help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.