设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 58|回复: 0

[制作教程] [TUTORIAL] Converting a variable to base64 and back

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    前天 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    4446

    主题

    864

    回帖

    2万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    22899
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    28239

    灌水之王

    发表于 4 天前 | 显示全部楼层 |阅读模式
    After reading a recent discussion on "save scumming" I decided that I'd like to improve my casino implementation by making the number of casino tokens illegible. Since MV and MZ use base64 to provide a basic form of game data protection I decided that perhaps the easiest way to avoid this kind of exploit is to also make the number of casino tokens a base64 value, decode it to make deductions or additions and then immediately convert it back to base64. Most likely I am going to do it at the immediate point where the calculation occurs in order to maximize the amount of data protection, and I may also consider stacking the base64 effect for additional protection.

    Anyway, here's the means to do this: btoa encodes to base64, and atob reverses the encoding. So for example, if you have 21 casino tokens saved to variable 77 and you want to encode the value to make it less susceptible to scumming then you would set the variable to use the following script evaluation:

                    JavaScript:       
    btoa($gameVariables.value(77))


    And the resulting value would be MjE=. To get back to 21 tokens you would then use atob() like this:

                    JavaScript:       
    atob($gameVariables.value(77))


    Note that this is not a perfect solution of course: if you have any text or other variable data that cannot be easily handled in an 8-bit value (read: anything in the Unicode range) then the game will throw an error and stop working. This isn't  the game's fault of course, just a limitation of the Javascript function. MDN has a complete explanation over here. Also, it's meant to make it more difficult to modify the variable even if you don't go through the hassle of encrypting the data, the latter of which would be somewhat more involved than this basic mechanism.

    Now here's where it gets interesting: what if you want to stack the base64 effect for extra protection of sensitive game data? Consider the following example, this time using a script evaluation:

                    JavaScript:       
    for (var i = 0; i <= 4; i++) {  data = btoa($gameVariables.value(77));  $gameVariables.setValue(77, data);}


    Starting from 0 (which in programming is actually 1) this counts to 4 (or rather 5) and stacks the encoding that many times. Now instead of the original, four-character value you have an insane, 32-character value of Vm10YWEyUXhTbk5SYkVwUlZrUkJPUT09. And to get back to 21 you use the same number of encoding steps that you used to stack the encoding as follows:

                    JavaScript:       
    for (var i = 0; i <= 4; i++) {  data = atob($gameVariables.value(77));  $gameVariables.setValue(77, data);  $gameVariables.setValue(77, parseInt($gameVariables.value(77)));}


    Note the addition of parseInt: this is a Javascript function which takes the returned value (which is a string of text) and parses out the numeric portion into an integer format (so that it's usable as a number). C++, C# and ObjectiveC programmers may consider it the Javascript version of int.Parse, however there is no int object in Javascript and so the parse function is called separately from an object designation. This is important because otherwise mathematical operations will either fail or append to the end of the number depending on the operation, so if you don't do this then something like 99999 + 18 will result in a value of 99999018 or similar instead of the expected result of 100017, which can send your calculations to infinity and beyond.


    本贴来自国际rpgmaker官方论坛作者:ZeroBreak处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/tutorial-converting-a-variable-to-base64-and-back.141686/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-7-14 14:29 , Processed in 0.061689 second(s), 55 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表