say I have the following snippet, where certificate
is a java Certificate object:
byte[] data = certificate.getEncoded(); String stringData = new String(Base64.encodeBase64(data)); byte[] newData = Base64.decodeBase64(stringData)
Is there any chance data
will not be equal to newData
? basically, is there loss of information to string and then back?
Thanks
Advertisement
Answer
Assuming nothing explodes due to out-of-memory errors or similar general problems, this code should be fine, with one big exception:
You are transforming the output of Base64.encodeBase64
(which is a byte[]
) to a String
without specifying the encoding.
This is generally speaking a bad idea, because it will use the platform default encoding.
In this specific case, it’s unlikely to be an actual problem. Because the output of Base64 contains only ASCII characters and almost all modern platforms use a ASCII-compatible encoding as their platform default encoding (for example Android, Mac OS X and most Linux distributions use UTF-8, Windows tends to use some code-page, but the lower 128 bytes are usually also ASCII compatible).
Still, it would be better to use new String(Base64.encodeBase64(data), StandardCharsets.US_ASCII)
just to be sure (for the reasons mentioned above, ISO_8859_1
and UTF_8
would work as well, but US_ASCII
is the “correct” pick).