Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Base64代码保持不变,即使其编码的值正在更改_Javascript_Html_Base64 - Fatal编程技术网

Javascript Base64代码保持不变,即使其编码的值正在更改

Javascript Base64代码保持不变,即使其编码的值正在更改,javascript,html,base64,Javascript,Html,Base64,我目前正在创建一个类似于cookieclicker的增量游戏。如果没有你可以破解的保存代码,cookie clicker会是什么。我有一个代码,它获取我的变量的所有值原子,点击值,elmts,分子,等等。它将它们放在一个带间隔的字符串中。然后将其编码为Base64。问题就从这里开始。起初,Bace64解码值与编码时相同,但如果更改值,则再次编码和解码,值与第一次相同。无论我尝试了多少次,这种情况都会持续发生 index.js: var data = atoms + "%-%" + clickVa

我目前正在创建一个类似于cookieclicker的增量游戏。如果没有你可以破解的保存代码,cookie clicker会是什么。我有一个代码,它获取我的变量的所有值
原子
点击值
elmts
分子
,等等。它将它们放在一个带间隔的字符串中。然后将其编码为Base64。问题就从这里开始。起初,Bace64解码值与编码时相同,但如果更改值,则再次编码和解码,值与第一次相同。无论我尝试了多少次,这种情况都会持续发生

index.js:

var data = atoms + "%-%" + clickValue + "%-%" + elmts + "%-%" + molecules;
//Clicking
var atoms = 15;
var clickValue = 1;

function atomClick() {
  atoms = atoms + clickValue;
  document.getElementById("atoms").innerHTML = abbrNum(atoms , true , 2);
};

//upgrades
function upgradeClickValue () {
clickValue = clickValue * 2;
};

//Auto click modifiers
//create veriables
var elmts = 0;
var molecules = 0;

function buyElement() {
    var elementCost = Math.floor(10 * Math.pow(1.1,elmts));
    if(atoms >= elementCost) {
        elmts++;
        atoms = atoms - elementCost;
        document.getElementById('elmts').innerHTML = abbrNum(elmts , true , 2);
        document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
    };
    var nextECost = Math.floor(10 * Math.pow(1.1,elmts));
    document.getElementById('elementCost').innerHTML = abbrNum(nextECost , true , 2);
};

function buyMolecule() {
    var moleculeCost = Math.floor(100 * Math.pow(1.2,molecules));
    if(atoms >= moleculeCost) {
        molecules++;
        atoms = atoms - moleculeCost;
        document.getElementById('molecules').innerHTML = abbrNum(molecules , true , 2);
        document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
    };
    var nextMCost = Math.floor(100 * Math.pow(1.2,molecules));
    document.getElementById('moleculeCost').innerHTML = abbrNum(nextMCost , true , 2);
};

window.setInterval(function() {

    atoms = atoms + (elmts * 1) + (molecules * 2);
    document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);

    document.title  = "AC Home :: Atoms: " + abbrNum(atoms , true , 2);

    var data = atoms + "%-%" + clickValue + "%-%" + elmts + "%-%" + molecules;
}, 1000);

//round numbers
const COUNT_ABBRS = [ '', 'K', 'M', 'B', 'T', 'Q', 'Qi', 'Se', 'Sp', 'Ot', 'No', 'De'];

function abbrNum(count, withAbbr = false, decimals = 2) {
    const i     = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000));
    let result  = parseFloat((count / Math.pow(1000, i)).toFixed(decimals));
    if(withAbbr) {
        result += `${COUNT_ABBRS[i]}`; 
    }
    return result;
}

function reset() {
    localStorage.removeItem("save")
    atoms = 0;
    clickValue = 1;
    elmts = 0
    elementCost = 10;
    molecules = 0;
    moleculeCost = 100;
}

// base64 encoding
var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}

//set code
var data = atoms + "%-%" + clickValue + "%-%" + elmts + "%-%" + molecules;
var encodedString = Base64.encode(data);

function save() {
    // Encode the String
    var encodedString = Base64.encode(data);
    console.log(encodedString);
    alert("Save Code: " + encodedString);
}
function load() {
    // Decode the String
    var decodedString = Base64.decode(encodedString);
    console.log(decodedString);
    var splitRst = decodedString.split("%-%");
    console.log(splitRst)
    alert("Values: " + splitRst);
} 
var数据=原子+“%-%”+点击值+“%-%”+elmts+“%-%”+分子;
//点击
var原子=15;
var-clickValue=1;
函数atomClick(){
原子=原子+点击值;
document.getElementById(“atoms”).innerHTML=abbrNum(atoms,true,2);
};
//升级
函数升级ClickValue(){
clickValue=clickValue*2;
};
//自动单击修改器
//创建可验证项
var-elmts=0;
var分子=0;
函数buyElement(){
var elementCost=数学地板(10*数学功率(1.1,elmts));
如果(原子>=元素成本){
elmts++;
原子=原子-元素成本;
document.getElementById('elmts').innerHTML=abbrNum(elmts,true,2);
document.getElementById('atoms').innerHTML=abbrNum(atoms,true,2);
};
var nextECost=数学地板(10*数学功率(1.1,elmts));
document.getElementById('elementCost').innerHTML=abbrNum(nextECost,true,2);
};
功能分子(){
var moleculeCost=数学下限(100*数学功率(1.2,分子));
如果(原子>=分子成本){
分子++;
原子=原子-分子成本;
document.getElementById('molecles').innerHTML=abbrNum(molecles,true,2);
document.getElementById('atoms').innerHTML=abbrNum(atoms,true,2);
};
var nextMCost=数学下限(100*数学功率(1.2,分子));
document.getElementById('molecleCost').innerHTML=abbrNum(nextMCost,true,2);
};
setInterval(函数(){
原子=原子+(elmts*1)+(分子*2);
document.getElementById('atoms').innerHTML=abbrNum(atoms,true,2);
document.title=“AC Home::Atoms:”+abbrNum(Atoms,true,2);
var数据=原子+“%-%”+点击值+“%-%”+elmts+“%-%”分子;
}, 1000);
//整数
const COUNT_ABBRS=['''K','M','B','T','Q','Qi','Se','Sp','Ot','No','De'];
函数abbrNum(计数,带abbr=false,小数=2){
const i=0==count?count:Math.floor(Math.log(count)/Math.log(1000));
让result=parseFloat((count/Math.pow(1000,i)).toFixed(小数));
if(带缩写){
结果+=`${COUNT_ABBRS[i]}`;
}
返回结果;
}
函数重置(){
localStorage.removietem(“保存”)
原子=0;
单击值=1;
elmts=0
要素成本=10;
分子=0;
分子成本=100;
}
//base64编码
本研究的目的是根据《美国参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考参考[123454589++2889+/=”,编码:“,编码:1,编码:编码:编码:功能(e)1234545454589+//,,,,,,编码:编码:编码:函数(e,编码:功能(e)为.编码:函数(e)5.5.5.6;编码:函数(e)编码:函数(e)编码:功能(e)功能(e)函数(e){{{{t::{t,;编码:函数(e)功能(钥匙字符(u)+这个。u钥匙字符(a)}返回t},解码:函数(e){var t=“”;var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^a-Za-z0-9+/=]/g,”);while(f>6&63|128);t+=String.fromCharCode(r&63|128)}返回t},{u utf8u解码:函数(e){var t=“”;var n=0;var r=c1=c2=0;while

为什么你不能这样做:

const data = {
    atoms: atoms,
    molecules: molecules,
    elements: elmts,
    clicks: clickValue 
};
const text = JSON.stringify(data);
const base64 = btoa(text);
然后解码

const text = atob(base64);
const data = JSON.parse(text);
consol.log(data.clicks);  // your click value here

如果不奇怪地加入
%-%
你的问题是
setInterval
函数中
data=
之前的
var
。你正在跟踪全局
数据
,并写入一个局部变量,该变量一旦函数结束就不再存在。

Base6中这个巨大的单行函数是什么4
为什么不使用
atob
btoa
atob
btoa
?是的,这只对字符串有效,但我认为这就足够了。不管怎样,可以假设巨大的不可读的一行程序实际上产生了预期的正确base64吗?是的,我相信再次解决这个问题,我认为我建议只使用
btoa
并将其用作数据格式。重新发明轮子通常不是一个好主意。你能将代码与它集成到我的代码中吗?它不做任何事情,只接受弹出的错误。
const text = atob(base64);
const data = JSON.parse(text);
consol.log(data.clicks);  // your click value here