error with characters when decoding a variable from js to php

0

Hello, I am a problem that I do not know the reason, I have a code encoded in base64 and I inserted this code into a js variable, from php to js, it worked more perfect impossible, then I passed this js variable to php, also worked beauty, it displays exactly the code after encoding, the problem was when decoding

Encode like this:

$encode = base64_encode(salt.hash));

I went to js like this:

window.localStorage.setItem("campo", "<?php echo $encode; ?>");

I switched to PHP like this:

$decode_js = "<script>document.write(localStorage.getItem('campo'))</script>";

Decode and display like this:

echo $decode = base64_decode($decode_js);

In this echo, what it brings is something very monstrous:

That:

  

MGszbGk5MGlvb2Y0ZWhwdXNkc2JhYWNmOTNlZTUyZTBlNjI0ZmQ0NzNkZjUyYjVlZDE5N2Y0ODM5NTQ0OWZmN2YxZjY0ZDg2ZDgzZWQ0YjBlNmRlNTFiYTk2ZmQ2YjA1ZjU1MjI5MTFmM2Q2OWI5N2RiMGUwMzhlYTg0OWZmYzBjZThiYWQxMDMxZjUzMmRkYWI0NzFiOTAzZA

Turned this:

  

hr 鞞 + ץ ƥJ + j z -zh >)

Can anyone help me with anything? Thank you in advance

    
asked by anonymous 12.07.2015 / 15:24

1 answer

1

The problem is that you are decoding the function of javascript and not the output of document.write, because php sends the information to the server first, then run the javascript. I kept the output in javascript:


<script type="text/javascript">
<?php 
$salt_rash = 'seucodigo';
$encode = base64_encode($salt_rash);
?>

function setCodigo() {
   localStorage.getItem('campo', '<?php echo $encode?>');
}

function getCodigo() {
 var codigo = base64_decode_js(localStorage.getItem('campo'));
 document.write(codigo);
}

function base64_decode_js(data) {
 var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
    ac = 0,
    dec = '',
    tmp_arr = [];

  if (!data) {
    return data;
  }

  data += '';

  do { // unpack four hexets into three octets using index points in b64
    h1 = b64.indexOf(data.charAt(i++));
    h2 = b64.indexOf(data.charAt(i++));
    h3 = b64.indexOf(data.charAt(i++));
    h4 = b64.indexOf(data.charAt(i++));

    bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;

    o1 = bits >> 16 & 0xff;
    o2 = bits >> 8 & 0xff;
    o3 = bits & 0xff;

    if (h3 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1);
    } else if (h4 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1, o2);
    } else {
      tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
    }
  } while (i < data.length);

  dec = tmp_arr.join('');

  return dec.replace(/

<script type="text/javascript">
<?php 
$salt_rash = 'seucodigo';
$encode = base64_encode($salt_rash);
?>

function setCodigo() {
   localStorage.getItem('campo', '<?php echo $encode?>');
}

function getCodigo() {
 var codigo = base64_decode_js(localStorage.getItem('campo'));
 document.write(codigo);
}

function base64_decode_js(data) {
 var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
    ac = 0,
    dec = '',
    tmp_arr = [];

  if (!data) {
    return data;
  }

  data += '';

  do { // unpack four hexets into three octets using index points in b64
    h1 = b64.indexOf(data.charAt(i++));
    h2 = b64.indexOf(data.charAt(i++));
    h3 = b64.indexOf(data.charAt(i++));
    h4 = b64.indexOf(data.charAt(i++));

    bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;

    o1 = bits >> 16 & 0xff;
    o2 = bits >> 8 & 0xff;
    o3 = bits & 0xff;

    if (h3 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1);
    } else if (h4 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1, o2);
    } else {
      tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
    }
  } while (i < data.length);

  dec = tmp_arr.join('');

  return dec.replace(/%pre%+$/, '');
}
</script>

<body onload="setCodigo();getCodigo();">
+$/, ''); } </script> <body onload="setCodigo();getCodigo();">
    
13.07.2015 / 16:44