admin管理员组

文章数量:1313812

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: /

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.

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/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.

Share Improve this question edited May 3, 2016 at 11:39 juanjo75es asked May 3, 2016 at 0:14 juanjo75esjuanjo75es 2621 gold badge4 silver badges16 bronze badges 6
  • 2 So you're pasting binary data and expect that text inputs will accept them successfully? – zerkms 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? – guest271314 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. – Sammitch 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. – juanjo75es Commented May 3, 2016 at 9:14
  • @guest271314 If you read two lines bellow that line you'll see – juanjo75es Commented May 3, 2016 at 9:15
 |  Show 1 more ment

1 Answer 1

Reset to default 5

Try using this mand 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/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="

本文标签: base64 encoding in javascript decoding in phpStack Overflow