I need the exact base64_decode() equivalent in javascript. I did a lot of research but have encountered some problems.
I can not use javascripts atob(), as for some reason, it just returns a complete different result as phps base64_decode().
Then I found a special decode function on this page: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
function Base64Decode(str, encoding = 'utf-8') {
var bytes = base64js.toByteArray(str);
return new (TextDecoder || TextDecoderLite)(encoding).decode(bytes);
}
function Base64Encode(str, encoding = 'utf-8') {
var bytes = new (TextEncoder || TextEncoderLite)(encoding).encode(str);
return base64js.fromByteArray(bytes);
}
Which indeed gives the same result, as the php function base64_decode(), but unfortunately, you can not reverse it.
So in php you can do base64_encode(base64_decode( 'BASE_64_ENCODED_STRING' ))
and you will get back your original BASE_64_ENCODED_STRING
but in Javascript
Base64Encode(Base64Decode( 'BASE_64_ENCODED_STRING' ))
won't give you back your original BASE_64_ENCODED_STRING, but I need to be able to do that. I found out, that it might be due to some UTF-8 encoding issue.
Interestingly btoa(atob( 'BASE_64_ENCODED_STRING' ))
will get me back the original BASE_64_ENCODED_STRING, but as I mentioned above, it won't decode the same as phps base64_decode() anyway...