1

I'm developing a website in Javascript. I've already made the backend using a REST api. To login on the server I have to decode a base64 string. Once decoded it should be just a binary string (not a simple text string, a binary string)

The problem is that when I do

function base64_decode(string) {
   return decodeURIComponent(Array.prototype.map.call(atob(string), function(c) {
      return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
   }).join(''));
}

I have this error : URIError: URI malformed. In addition with atob and btoa I've had wrong base64 strings.

I've tried many different algorithms, with many different packages and it still does not work. Is it possible in Javascript to decode a base64 encoded binary string ?

I've used the same thing for the iOS application and with Swift it works fine. It works also fine in Java. I just struggle with Javascript on the base64 decoding.

An example of a string I'm trying to decode lYlRMvG3MuhOqjmCPAHnoQmEH/TS087iZj9zt6vMG+nJhRsaqr/oTw+drfM+KAEx1xwWbFu6WGB8uEyKPZLb7n9f5mToKiLf8gVVBpogGsI8/EYz3xsxSbLjLby8+FSLNYV8HA4Ct6N5oGEVyM58CY+HwuKZ0T6k7r+t1NDU584=

  • 1
    Could you provide a sample of the input string? Sometimes these strings are encoded as a blob ("data:image/png;base64,iVBORw0KG...") – vbguyny Nov 17 '16 at 20:45
  • Just updated the post to include a base64 string example – GitCommit Victor B. Nov 17 '16 at 20:47
  • 1
    When you mentioned binary string, meaning that this contains binary data. In my experience you can only do this in JavaScript with the use of Blobs. You can refer to this the following link for additional information: http://stackoverflow.com/a/16245768/1640090 Good luck! – vbguyny Nov 17 '16 at 20:51

0 Answers0