John Davidson

Base64 Decode - Javascript - PHP

0 comments
Message:


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)



My Answer:
The issue here is likely due to the different character encoding used in PHP and JavaScript.

In PHP, when you use base64_decode, it decodes the base64 string into a binary string. When you echo or print this binary string, PHP will interpret it as ASCII characters by default.

In JavaScript, when you use Buffer.from with "ascii" encoding, it will interpret the binary string as ASCII characters as well. However, JavaScript may handle certain characters differently than PHP, leading to the discrepancy in the decoded string.

To ensure consistency between PHP and JavaScript decoding, you can try using "utf-8" encoding in JavaScript instead of "ascii":

javascript
const key = Buffer.from(key, "base64").toString("utf-8");


This should give you a decoded string that is more similar to the one you get in PHP.

Rate this post

4 of 5 based on 4133 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us