简体   繁体   English

php中javascript解码中的base64编码

[英]base64 encoding in javascript decoding in php

I am trying to encode a string in javascript and decode it in php.我正在尝试在 javascript 中编码一个字符串并在 php 中对其进行解码。

I use this code to put the string in a inputbox and then send it via form PUT.我使用此代码将字符串放入输入框中,然后通过表单 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/这里有一个用于编码页面的 jsfiddle: https ://jsfiddle.net/okaea662/

The problem is that I always get a string 98% correct but with some different characters.问题是我总是得到一个 98% 正确的字符串,但有一些不同的字符。 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%YE {\\ö°(úýýÁñ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 |S÷ä½m0×C | U>£áWÅàUù»¥ïs7Dþ1Ji%YE {\\ö°(úýýÁñ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.请注意,第 4 个字符是不同的,然后在某处还有一两个字符。 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)我在标题和元数据中使用 Chrome 浏览器和 utf-8 编码(Firefox 似乎在输入框中使用不同的编码,但我稍后会查看该问题)

EDIT:编辑:

The encoding to base64 apparently is not the problem.对 base64 的编码显然不是问题。 The base64 encoded string is the same in the browser than in the server.浏览器中的 base64 编码字符串与服务器中的相同。 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.如果我在 javascript 中对它进行 base64 解码,我会得到原始字符串,但是如果我在 PHP 中对其进行解码,我会得到一个稍微不同的字符串。

EDIT2: I still don't know what the problem is but I have avoided it sending the data in a blob with ajax. EDIT2:我仍然不知道问题是什么,但我已经避免使用ajax将数据发送到blob中。

Try using this command to encode your string with js:尝试使用此命令用 js 对您的字符串进行编码:

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

Now with php , you simply use: base64_decode($signature)现在使用php ,您只需使用: base64_decode($signature)

If that doesn't work (I haven't tested it) there may be something wrong with the btoa func.如果这不起作用(我还没有测试过),那么btoa func 可能有问题。 So checkout this link here:因此,请在此处查看此链接:

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding 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="

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM