5

I am trying to encode a string in javascript and decode it in php.

I use this code to put the string in a inputbox and then send it via form PUT.

document.getElementById('signature').value= b64EncodeUnicode(ab2str(signature));

And this code to decode

$signature=base64_decode($signature);

Here there is a jsfiddle for the encoding page: https://jsfiddle.net/okaea662/

The problem is that I always get a string 98% correct but with some different characters. For example: (the first string is the string printed in the inputbox)

¦S÷ä½m0×C|u>£áWÅàUù»¥ïs7Dþ1Ji%ýÊ{\ö°(úýýÁñxçO9Ù¡ö}XÇIWçβÆü8ú²ðÑOA¤nì6S+̽ i¼?¼ºNËÒo·a©8»eO|PPþBE=HèÑqaX©$Ì磰©b2(Ðç.$nÈR,ä_OX¾xè¥3éÂòkå¾ N,sáW§ÝáV:ö~Å×à<4)íÇKo¡L¤<Í»äA(!xón#WÙÕGù¾g!)ùC)]Q(*}?­Ìp

¦S÷ ä½m0×C|u>£áWÅàUù»¥ïs7Dþ1Ji%ýÊ{\ö°(úýýÁñxçO9Ù¡ö}XÇIWçβÆü8ú²ðÑOA¤nì6S+̽ i¼?¼ºNËÒo·a©8»eO|PPþBE=HèÑ qaX©$Ì磰©b2(Ðç.$nÈR,ä_OX¾xè¥3éÂòkå¾ N ,sá W§ÝáV:ö~Å×à<4)íÇKo¡L¤<Í»äA(!xón#WÙÕGù¾g!)ùC)]Q(*}?­Ìp

Note that the 4th character is distinct and then there is one or two more somewhere. The string corresponds to a digital signature so these characters make the signature to be invalid.

I have no idea what is happening here. Any idea? I use Chrome browser and utf-8 encoding in header and metas (Firefox seems to use a different encoding in the inputbox but I will look that problem later)

EDIT:

The encoding to base64 apparently is not the problem. The base64 encoded string is the same in the browser than in the server. If I base64-decode it in javascript I get the original string but if I decode it in PHP I get a slightly different string.

EDIT2: I still don't know what the problem is but I have avoided it sending the data in a blob with ajax.

6
  • 2
    So you're pasting binary data and expect that text inputs will accept them successfully? Commented May 3, 2016 at 0:17
  • "I am trying to encode a string in javascript" Encode as which format? Not certain what requirement is? Commented May 3, 2016 at 0:24
  • The problem is neither the encoding in JS, nor the decoding in PHP. The problem is that you're trying to copy and paste binary data from an external source into an HTML form. The sane approach* would be to make the external source encode the data in base64 [or another 7-bit-safe encoding] in order to ensure that the data makes its way through the system unmolested. Commented May 3, 2016 at 0:41
  • @zerkms I'm not sending bibary data. I'm sending base64 encoded data. That's the purpose of encoding: not sending binary data. Commented May 3, 2016 at 9:14
  • @guest271314 If you read two lines bellow that line you'll see Commented May 3, 2016 at 9:15

1 Answer 1

5

Try using this command to encode your string with js:

var signature = document.getElementById('signature');
var base64 = window.btoa(signature);

Now with php, you simply use: base64_decode($signature)

If that doesn't work (I haven't tested it) there may be something wrong with the btoa func. So checkout this link here:

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

There is a function in there that should work (if the above does not)

function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    }));
}

b64EncodeUnicode(signature); // "4pyTIMOgIGxhIG1vZGU="
Sign up to request clarification or add additional context in comments.

2 Comments

I already use b64EncodeUnicode. I have also tried with btoa and some other encoding functions. Always similar problems.
Base64 is unsafe to render in a browser or to pass in a URL. See stackoverflow.com/questions/42165891/… for a safe way to encode/decode arbitrary strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.