1

So I'm confused. I know there's lots of Base64 encoders/decoders for JS, but not for the modified, So far searching across stackoverflow has come up dry. I was trying something on BLOGGER to make simple encryption script using btoa() and atob() JS methods to encrypt and then decrypt again the URL On Desktop browsers it goes very well and the original link/url regenerated successfully but when trying on mobile browsers because of this suffix added by blogger at the end of the url (?m=1) that cause all things messy and the final link does not give the original link

Here my codes

// that converts the original url to base64 through btoa()
    <input id="myinput" type="text" name="search" />
   <input id="shrt" type="submit" value="Shorten" onclick="myFunction()" />
   <textarea id="m"></textarea> 
   <script>
      function myFunction() {
         var mm = document.getElementById("myinput").value;
         var b64 = btoa(mm);
         document.getElementById('m').innerHTML = "https://basebb.blogspot.com/p/go.html?url=" + b64;};
      </script>

then I use the window.location.href to get the link and generate another link the the base64 encryption

    <a id="lnk" href="" style="font-size: 40px;">FirstLink</a>
 <script>
 var str = window.location.href;
 var res = str.substr(42, );
 var a = document.getElementById('lnk');
 a.href = "https://basebb.blogspot.com/2018/09/art.html?url=" + res;
</script>

and finally I use atob() to recover the original url here

   <a id="lnk" href="" style="font-size: 40px;">FinalLink</a>
     <script>
        var str = window.location.href;
        var str1 = str.substr(49, );
        var a = document.getElementById('lnk');
        a.href = atob(str1);
     </script>

trying for (https://twitter.com/) it gives me (https://basebb.blogspot.com/p/go.html?url=aHR0cHM6Ly90d2l0dGVyLmNvbS8=)

as I said it works well on desktop but on mobile after second redirect it gives me that

(https://basebb.blogspot.com/p/go.html?url=aHR0cHM6Ly90d2l0dGVyLmNvbS8%3D&m=1)

note that (%3D&m=1) at the ending it causes the all problem

the other problem is that this (%3D&m=1) changed with every link changed in length and characters also

I tried many methods like substr(), slice(), replace(), but no one works and the problem still exists

please any help?? and sorry for my bad English

Sami
  • 29
  • 9
  • Btw, base64 is an *encoding*, not an encryption. And for use of strings in URLs, `encodeURIComponent`/`decodeURIComponent` would be much more appropriate. – Bergi Sep 17 '18 at 17:39
  • @Bergi thanks for your reply .. but can you give me more details how to use encodeURIComponent / decodeURIComponent .. ? – Sami Sep 17 '18 at 17:49
  • Exactly like you already used `atob` and `btoa`, it's just a different encoding. – Bergi Sep 17 '18 at 17:52
  • I will make a try and I hope it solve this problem, I will talk to you again, thanks – Sami Sep 17 '18 at 17:58
  • No, see the duplicate topic for the solution of the problem. Using URL encoding instead of base64 was just additional advice. – Bergi Sep 17 '18 at 18:00
  • I just tried it now, encodeURIComponent it makes the original url still readable and in my case I need to encrypt/encode/hide the name/url so I think btoa() is more appropriate but still doesn't work with mobile also @Bergi I saw the link you mentioned but found too many answers that made me more confused .. Is not there any quick help? – Sami Sep 17 '18 at 18:05
  • `btoa` is at best an obfuscation - every determined user can convert it back. It's not an encryption, "hiding" the original value is not secure by this means. – Bergi Sep 17 '18 at 18:07
  • sorry for my mis-understand ... waiting for any quick code to try for mobile users ... – Sami Sep 17 '18 at 18:13

0 Answers0