I recently figured out how tricky a base64 validation can be, but voila.. after googling out much i found a few rules that can be put.
There are multiple rules which are required to validate a Base64 string and these validation Rules can be put in any programming language.
- Validate if the String contains only the characters “=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”.
- Base 64 string has length in multiples of 4 only.
- “=+/” characters must be at the end of the Base64 String.
- = can be at the most twice at the end of the string.
- There may be multiple occurrences of any of the other characters than ‘=’.
For exact validation if the data Encoded in the Base64 string is fine or not, the Data string needs to be validated after Decoding the base64 String. Moreover, since the data contained in the base64 encoded string can be anything, i.e. Picture, Text, Document, PDF, etc., you may need to put some additional algorithm to validate the Encoded data in it.
For further details about base64 format, the attached document can be referred.