Closed nijinekoyo closed 1 month ago
Your sample appears to be the result of Base64.decode()
instead of Base64.encodeURL()
. And it also appears to be used by some other framework. Would you try again with less complicated environment with clearer example? Would you also check the environment where this module is used, since that may be the cause of your problem instead of js-base64?
This is a minimal example using the vue3 framework that will print incorrect base64 on the page
<template>
<div>
{{ Base64.encodeURL(JSON.stringify(testData)) }}
</div>
</template>
<script setup lang="ts">
import { Base64 } from 'js-base64';
let testData = {
"captcha_id": "7229bda06297c38e1d5d43fcb154ff07",
"lot_number": "dc1dbe209ad64b72a86d09f830a7c439",
"pass_token": "e1a45d5d3c7201a59ee178138868cd5b38972b583fb5b0e7665ff00146fffb1e",
"gen_time": "1726316639",
"captcha_output": "JGE2dsn1dAz4XFoeOPS8O_ISPSM-usiVxL5L_MM4usM3chjlc3jDizP4hdQ9srwXoG3oDmnGpBwAvEx-l16XvAaEyx2V3bNo-GZ5H6reyHolX9ZeZmcg7xwaOE6xAb27gN0cLZkOGYhoeOYnQ4EmQYnAh2NZ0eb175QsgoZOGSVMp6jMxR4iUPkPt1rGZI8UxUTY4FOvhKEWylOJErjMAvIB6BouUVs2NQakF1fBJVm0qI50P6O2HWG1twJ52MD4CVDDqMjJ3FGyBxcPUGAeSMANAIq0lmd0N4TedGZd1cbXueHvbVnZ-yHznP2nO2EE_9GOqeDB7Pqo6-CqKFl7ZdNtuKI9ZwA_8b-9CzoZiXax-jkc4cbTJo-Y5Av5LujdjR0CgX3D-x7JCmIX4smyUO-pV-wNqJOhmUPy2jGT3VzJToa83wRkxUZrWeF6I7hbZ-eTwEZOPlUNi_5tNKPJtaK3SLYGuBkDkd3Z6a67zi27EITIUqjMlvsGLXu5RFkC"
}
</script>
In your screen shot Alphabet is defined as A-Za-z0-9+/=
which is a valid set of Base64 character, and an invalid Base 64 Encoding with URL and Filename Safe Alphabet. It is A-Za-z0-9\-_=
that is correct (note \
is an escape char).
https://datatracker.ietf.org/doc/html/rfc4648#section-5
Would you try replacing the Alphabet definition and see what happens?
Oh sorry, I forgot to switch when I took the screenshot, but switching the character set has the same effect
I think I understand the cause of the problem. If the original text is base64 and there are _
or -
characters, an incorrect result will be generated.
On mobile devices, using
Base64.encodeURL
to encode json content cannot be parsed using strict mode PC Generated:Mobile device Generated:
This problem only occurs on mobile devices. It cannot be reproduced on PC even with mobile device simulation This problem occurs on both Android and iOS devices. Test browsers include Safari, Chrome, Edge