Base64 编码与 Ascii85 编码

2022-01-21 00:00:00 json base64 java ascii85

我的工作项目是使用 Jackson JSON 序列化程序将一堆 Java 对象转换为字符串,以便将它们发送到 REST 服务.

My project at work is using the Jackson JSON serializer to convert a bunch of Java objects into Strings in order to send them to REST services.

其中一些对象包含敏感数据,所以我编写了自定义序列化程序来将这些对象序列化为 JSON 字符串,然后 gzip 它们,然后使用 AES 加密它们;

Some of these objects contain sensitive data, so I've written custom serializers to serialize these objects to JSON strings, then gzip them, then encrypt them using AES;

这会将字符串转换为字节数组,因此我在 中使用 Base64 编码器Apache commons 编解码器将字节数组转换为字符串.REST 接口背后的自定义反序列化器反转了这个过程:

This turns the strings into byte arrays, so I use the Base64 encoder in Apache commons codec to convert the byte arrays into strings. The custom deserializers behind the REST interfaces reverse this process:

base64 解码 ->解密->解压->使用默认的 Jackson 反序列化器进行反序列化.

Base64 编码会增加输出的大小(序列化中的 gzip 步骤旨在帮助改善这种增加),所以我检查了谷歌,看看是否有更有效的替代方案,这导致我this 之前提出 Ascii85 编码作为更有效的替代方案 -

Base64 encoding increases the size of the output (the gzip step in serialization is meant to help ameliorate this increase), so I checked Google to see if there was a more efficient alternative, which led me to this previous stackoverflow thread that brought up Ascii85 encoding as a more efficient alternative -

Base64 将输出大小增加 33%,Ascii85 将输出大小增加 25%.

Base64 adds 33% to the size of the output, Ascii85 adds 25% to the size of the output.

我发现了一些 Java Ascii85 实现,例如Apache pdfbox,但我对使用编码有点怀疑——似乎几乎没有人在使用或实现它,这可能只是意味着 Base64 有更多的惯性,或者这可能意味着 Ascii85 存在一些不稳定的问题.

I found a few Java Ascii85 implementations e.g. Apache pdfbox, but I'm a bit leery to use the encoding - it seems like hardly anybody is using or implementing it, which might just mean that Base64 has more inertia, or which may instead mean that there's some wonky problem with Ascii85.

有人知道更多关于这个主题的信息吗?Ascii85 是否有任何问题意味着我应该改用 Base64?

Does anybody know more on this subject? Are there any problems with Ascii85 that mean that I should use Base64 instead?

推荐答案

Base64 是 way 更常见.在大多数情况下,大小的差异实际上并不显着,如果您在 HTTP 级别(将压缩 base64)而不是在您的有效负载中添加 ,您很可能会发现差异完全消失了.

Base64 is way more common. The difference in size really isn't that significant in most cases, and if you add at the HTTP level (which will compress the base64) instead of within your payload, you may well find the difference goes away entirely.

Ascii85 是否有任何问题意味着我应该改用 Base64?

Are there any problems with Ascii85 that mean that I should use Base64 instead?

我强烈建议使用 base64,因为它所以更广泛.这几乎是将二进制数据表示为文本的规范方式(当然,除非您想使用十六进制).

I would strongly advise using base64 just because it's so much more widespread. It's pretty much the canonical way of representing binary data as text (unless you want to use hex, of course).

相关文章