最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - How can I obfuscate a string in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Jun 29, 2015 at 5:33 p1xelp1xel 3042 silver badges10 bronze badges 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. – Teemu Commented Jun 29, 2015 at 5:36
Add a ment  | 

2 Answers 2

Reset to default 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')

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);
发布评论

评论列表(0)

  1. 暂无评论