How to check that byte array is Base64 encoded?
I have an array of bytes, which I need to test if it is base64 encoded.
In base64 encoding, the character set is [A-Z, a-z, 0-9, and + /] . If the rest length is less than 4, the string is padded with '=' characters. ^([A-Za-z0-9+/]{4})* means the string starts with 0 or more base64 groups.
The ToBase64String method is designed to process a single byte array that contains all the data to be encoded. To encode data from a stream, use the System. Security.
Base64 does some padding for you using the = character at the end. If that padding is invalid, it's not a correct base64 encoding, even though it matches your regex. You can demo this by finding a base 64 string with 1 or 2 = at the end, removing them, and trying to decode it.
You can identify a binary using base64 encoding by looking for a long string comprising the Base64 character set (alphanumeric characters, + and / ).
You can use apache's Base64.isBase64(byte[]) method:
import org.apache.commons.codec.binary.Base64;
...
boolean isBase64 = Base64.isBase64(bytes);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With