Skip to content Skip to sidebar Skip to footer

Why Are Atob And Btoa Not Reversible

I'm trying to find a simple way to record and temporarily obfuscate answers to 'quiz' questions I'm writing in Markdown. (I'll tell the students the quiz answers during the present

Solution 1:

That is the reason.

In Base64 encoding, the length of output encoded String must be a multiple of 3. If it's not, the output will be padded with additional pad characters (=). On decoding, these extra padding characters will be discarded.

var string1 = "one",
  string2 = "one2";

console.log("Value of string1", string1)
console.log("Decoded string1", atob(string1))
console.log("Encoded string1", btoa(atob(string1)))
console.log("-------------------------------------")
console.log("Value of string2", string2)
console.log("Decoded string2", atob(string2))
console.log("Encoded string2", btoa(atob(string2)))

Solution 2:

As @george pointed out, one must use btoa() before using atob():

atob( btoa( 'hello' ) )

Solution 3:

btoa means binary to ascii: input is Binary=any kind of data: text, images, audio. Output is Ascii=its base64 encoding, which is an ascii subset, i.e. a text string containing only upper and lowercase letters, numbers, comma, plus, slash, equal sign (only for padding at end).

atob means ascii to binary: input MUST be a subset of Ascii, i.e. the result of a base64 encoded string. Output is Binary=any type of data (text, image, audio, ...).

Post a Comment for "Why Are Atob And Btoa Not Reversible"