4

Basically, I want to make a game in JavaScript and allow a user to get a copy paste-able code which stores their data. In reality, this "code" is actually obfuscated JSON that can be decoded by the application later.

I don't need much security, as I am aware that if people put some effort in they can view/modify the save, and I have no interest in stopping them. I just want the average user to not be tempted and/or see unnecessary information.

Thanks in advance.

1
  • 4
    Looks like you've obfuscated what you've tried so efficiently, that I couldn't find it from your question. Use that same powerful obfuscation in your app too. Commented Jun 29, 2015 at 5:36

2 Answers 2

5

you can use base64 encoding to encode your json String. it would be faster approach.

If you with pure javascript :

var encodedData = btoa("stringToEncode");

If you are using nodejs:

base-64 encoding:

var encodedStr = new Buffer("Hello World").toString('base64')

decode to original value:

var originalString = new Buffer("SGVsbG8gV29ybGQ=", 'base64').toString('utf-8')
Sign up to request clarification or add additional context in comments.

3 Comments

OP did not say anything abut using Nodejs. So I'd just suggest btoa().
OP could use Browserify if he really wanted it on the client
I am using it in the browser, no node.js.
1

Well... given that there is no security concern and you only want users to see what appears to be garbled data you can "encode" all the json data

var jsonData = {"key":"value"};

// turn it into a string
var jsonString = JSON.stringify(jsonData);

// replace some letters
var awkardString = jsonString.replace(/a/g, '!Ax6'); // be carefull, you should replace a letter with a pattern that does not already exist on the string.

// encode it with some type of reversible encoding
var garbledData = encodeURI(jsonString);

// output is: %7B%22key%22:%22v!Ax6lue%22%7D

// to "decode" it do the same steps in reverse
awkardString = decodeURI(garbledData);
jsonString = awkardString.replace(/!Ax6/g, 'a'); // now you see, if '!Ax6' existed on the source string, you would loose it and get an 'a' in return. That is why the replacement should be as unique as possible
jsonData = JSON.parse(jsonString);

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.