0

I have the next string with base 64 encoded:

aJjueq22q2O202ZzZdpjPO6Cr32p0cc22jeaceupcjd2Z2SuPg2J6rwupOQzS62e3cC2eWS2gC2P232a2p925w==

When I decoded with base64_decode function in PHP I received the next string:

$key = base64_decode($key);
.3K��yd-}ڟv7qr7vgd>

But in JS I did the next:

const key = Buffer.from(key, "base64").toString("ascii");

And I received a different decoded string:

h↑nz-6+cj<.$d3K-▲]@6yd6-☼[}→Z▼vgvgd.>

How is it possible? I need to get a string like the first (PHP)

  • 1
    In both cases you show, neither ends up as "ascii". So the PHP way results in byte code. And your js attempt wants to make it into "ascii". It's *not* ascii when decoded, otherwise it would be "readable" a lil bit? – mardubbles Apr 16 '22 at 08:38
  • `base64_encode` doesn't decode, it encodes. `base64_decode` decodes. – simon.ro Apr 16 '22 at 08:58
  • the base64 string is 88 characters long or 86 without padding. That's 512 bits or 64 bytes that are encoded in it. Why do you consider a wrongly to ASCII or UTF-8 decoded string of 19 characters including placeholder for unprintable characters as a correct result? What do you even expect to find in it? – jps Apr 16 '22 at 09:05
  • The only that I know is the decode that PHP does is working well, and I need to get the same decode in JS, how can I do? Thank you! – Matias Wajnman Apr 16 '22 at 09:19
  • Further, when I base64-decode in Notepad++, I get `h¶«c¶Ӧseڣ<}©ч6ڷq멲7vgd®> 꼮¤䳋­݀¶yd¶-۽ڟv`, and when I decode in one of them online tools, I get `hîz­¶«c¶ÓfseÚc<î¯}©ÑÇ6Ú7që©r7vgd®> ê¼.¤ä3K­ÝÀ¶yd¶-Û}Úvç`. And when I save the binary data into a file, and view with HxD, I get `h˜îz­¶«c¶ÓfseÚc<}©ÑÇ6Ú7šqë©r7vgd®> ‰ê¼.¤ä3K­žÝÀ¶yd¶€-Û}šÚŸvç` as the "decoded text". What do we learn from this? That binary data isn't meant to be viewed as an ASCII representation or coerced into ASCII. In JS, convert to blob? – Markus AO Apr 16 '22 at 09:24
  • @MatiasWajnman How do you define "working well", when you get a more or less random result of 19 characters after wrongly decoding 64 byte binary data to a 'readable' string? – jps Apr 16 '22 at 09:29
  • Im trying to adapt a PHP code in JS code and I have a lots of problems. Yesterday I created other post to talk about it [here](https://stackoverflow.com/questions/71877860/sign-a-message-with-eddsa-algorithm-in-javascript-to-get-jwt) . Today I tried again to find the problem and I saw that the output of base64_decode($key) in PHP is different than the output of my JS code key = Buffer.from(key, "base64").toString("ascii"). I understand that way is wrong but how can I get the same output of base64_decode($key) in JS? Maybe the string representation wasn't a good idea. – Matias Wajnman Apr 16 '22 at 09:47
  • If you want to compare the result of decoded binary data, you need to compare the actual binary data, not its (lossy) ASCII string representations. [Create a BLOB](https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript) in JS, save it, and compare it to the binary data decoded in PHP (either fetching the file contents in PHP and comparing, or using a hex editor) to verify that the (binary data of the) outcomes match. – Markus AO Apr 16 '22 at 09:54
  • The JavaScript counterpart to the PHP byte string would most closely be achieved with `.toString('binary')` instead of `.toString('ascii')`. Arbitrary binary data is usually corrupted when output to the console (charset encodings) and often displayed differently (different encodings and fonts). For comparison, a binary-to-text encoding like *hex* is best, e.g. `print(bin2hex($key))` or `console.log(Buffer.from(key, 'binary').toString('hex'))`. Regarding your [previous question](https://stackoverflow.com/q/71877860/9014097): handle the key as buffer rather than string for compatibility reasons. – Topaco Apr 18 '22 at 13:22

0 Answers0