为什么atob和btoa不可逆转 [英] Why are atob and btoa not reversible

查看:75
本文介绍了为什么atob和btoa不可逆转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种简单的方法来记录并暂时模糊我在Markdown写的测验问题的答案。 (我会告诉学生在演示期间的测验答案,所以我不是在寻找任何类型的安全加密。)

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 presentation, so I'm not looking for any kind of secure encryption.)

我以为我可以使用 atob('我要混淆的消息')然后告诉学生他们可以在他们的开发者工具面板中使用 btoa()来反转这个过程。但是以下不会返回'one':

I thought I could use atob('message I want to obfuscate') then tell students they can use btoa() in their developer tools panel to reverse the process. However the following does not return 'one':

btoa( atob('one') )

有谁知道为什么这不会返回'one'?是否有其他内置于JavaScript中的方法可以让人们对消息进行松散加密和解密? (我正在与绝对的初学者一起工作,他们可能会被功能混淆,并且在尝试将库添加到页面时会非常困惑。)

Does anyone know why this doesn't return 'one'? Are there other methods built into JavaScript that will allow one to loosely encrypt and decrypt a message? (I'm working with absolute beginners who might be confused by functions and who would be very confused trying to add libraries to a page).

推荐答案

这就是原因。


在Base64编码中,输出编码String的长度必须是
倍数3.如果不是,输出将用额外的
填充字符填充( = )。在解码时,这些额外的填充字符将被丢弃

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)))

这篇关于为什么atob和btoa不可逆转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆