Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that byte array is Base64 encoded?

Tags:

java

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.

like image 488
Bear Bear Avatar asked Feb 14 '17 12:02

Bear Bear


People also ask

How can I tell if data 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.

Is Base64 a byte array?

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.

How do I check if a string is valid Base64?

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.

How do I find Base64 code?

You can identify a binary using base64 encoding by looking for a long string comprising the Base64 character set (alphanumeric characters, + and / ).


1 Answers

You can use apache's Base64.isBase64(byte[]) method:

import org.apache.commons.codec.binary.Base64;

...

boolean isBase64 = Base64.isBase64(bytes);
like image 144
Boris Avatar answered Oct 18 '22 11:10

Boris