经常在程序员混杂的技术论坛中, 大部分技术人员为了保护自己的隐私会, 会对一些敏感信息进行加密.比如:

1
xxx@gmail.com

然后base64加密一下, 非常帅气地甩出

1
eHh4QGdtYWlsLmNvbQ==

虽然, 搜索引擎的爬虫直接可以解开这种加密并收录, 但这不是今天讨论的重点.

重点是每次想要查看这样的加密的信息时候, 就需要解开base64.这时候聪明的程序师们就会使用开发者工具, 用js临时解一下

1
2
> btoa("eHh4QGdtYWlsLmNvbQ==")
< "ZUhoNFFHZHRZV2xzTG1OdmJRPT0="

MMP, 怎么没有解开, 我去!!!又打反了, 应该是atob,你有没有想过这是为什么老是打反呢.

1
2
> atob("eHh4QGdtYWlsLmNvbQ==")
< "[email protected]"

为什么这个名字设计得这么简单, 以至于不好记呢?

在w3c的文档中, 关于web Application APIs的base64方法记录中, 有这么一段解释

NOTE:

In these APIs, for mnemonic purposes, the “b” can be considered to stand for “binary”, and the “a” for “ASCII”. In practice, though, for primarily historical reasons, both the input and output of these functions are Unicode strings.

解释到’b’代表’binary’, ‘a’代表’ASCII’, 那就明了了.

那什么Base64是ASCII(a), 不是(b)呢

很简单, 看一下base64的定义就知道了

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation.

from https://en.wikipedia.org/wiki/Base64

Base64, 首先是一个编码方案, 是二进制到文本的编码方案, 是用以ASCII字符串格式表示二进制数据。

所以, btoa是指的是编码转换这个行为, 而其默认的编码方案是Base64.

破案