Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How atob doesn't convert from Buffer with base64?

I have data that I encrypt using lz-string package.

I also convert the result to base64 and using atob() function to convert from base64.

The problem is atob() doesn't work as expected but Buffer.from(b64, 'base64').toString(); does.

Why? how do I fix that? I need to use atob in the client side (the Buffer is not exist in the browser).

StackBlitz example

like image 743
Jon Sud Avatar asked Nov 01 '25 06:11

Jon Sud


1 Answers

Use decodeURIComponent and escape to convert to UTF-8.

const non64 = decodeURIComponent(escape(window.atob( b64 )));
like image 151
Shlomi Levi Avatar answered Nov 02 '25 20:11

Shlomi Levi