GregV
GregV

Reputation: 39

How to know what variant of Base64 encoding Java class uses?

In my Java program i use com.ibm.xml.enc.dom.Base64 class for encoding/decoding
binary files. How can i know what variant of Base64 encoding this class uses?

Upvotes: 0

Views: 779

Answers (1)

Luc
Luc

Reputation: 1433

You have indeed several implementations for base64.

The idea behind this encoding, is to find a way to carry raw bytes through the different network layers, without them to be altered. Each layer is reading bytes, and you don't want your raw data to be cut (corrupted) because a random sequence of bytes is interpreted like a "end-of-rquest". That's why application data are encoded as printable characters. (more details: http://www.comptechdoc.org/independent/networking/protocol/protlayers.html)

Most of the Base64 tables are using A-Z, a-z and 0-9 for the 62 first characters. And the differences among implementations are for the last 2 characters and the padding one.

The most common implementations use + and / for the last two characters of the table. But you might find as well - and _ which are used to be url-safe.

For your class com.ibm.xml.enc.dom.Base64, nothing is specified in the doc: https://www.ibm.com/support/knowledgecenter/en/SSYKE2_6.0.0/com.ibm.java.security.api.doc/xmlsec/com/ibm/xml/enc/dom/Base64.html#Base64()

So you can assume that they use the most common implementation for Base64. If you have doubt, just try to generate example with random raw bytes. You can double-check that the base64 are using + and / and 63 and 64 characters.

If you need to write a generic base64 decoder, able to handle different variant of bas64. You would need to check for these special characters, check the length of the string, and the characters used for padding. From these info you could deduce an implementation to be used.

You have much more details about the variants on wikipedia: https://en.wikipedia.org/wiki/Base64

Upvotes: 2

Related Questions