JavaScript atob operation using PHP

26,047

Solution 1

Have a look at base64_decode().

JavaScripts btoa() just encodes a string using Base64. The PHP functions for that are base64_encode() and base64_decode().

Solution 2

When I use window.btoa(String) to encode (not encrypt) text and send it over to the server side via AJAX, I find that the client-server exchange has resulted in plus signs ('+'), in the encoded text, being replaced by spaces (' ').

To get the text back to proper encoding in PHP, I've had to use string transform like so:

$clean = strtr( $_POST['ajax-text'], ' ', '+');
$ascii = base64_decode( $clean );
Share:
26,047

Related videos on Youtube

Alfred
Author by

Alfred

I am a Full Stack developer and a DevOps Engineer, who always to learn from, and contribute to, the technology community. I was a beginner in programming once. What all I had was pieces of scattered and crude knowledge (don't know if i can call it knowledge) then. Later, I joined for Master of Computer Applications in a college and there, I got a great teacher. It was her, who taught me when and where to use 'while' and 'for' loops even.What all knowledge I have now, and what all achievements I've made till now, is only because of her. Compared to her, I am ashes. Hats off my dear teacher Sonia Abraham, you are the best of your kind. Sonia Abraham is a professor of the Department of Computer Applications, M.A College of Engineering, Mahatma Gandhi University

Updated on July 05, 2022

Comments

  • Alfred
    Alfred almost 2 years

    I would like to know if it is possible to decrypt the JavaScript encrypted text (which is encrypted using JavaScript's btoa function), using PHP.

  • Sam
    Sam about 11 years
    This is a comment not an answer
  • Dogbert
    Dogbert about 11 years
    @Sam, what's the answer then?
  • deltab
    deltab over 3 years
    Sounds like you've sent the encoded text in the query string of a URL. You should encode it using encodeURIComponent before inserting it.